C# asp.net mvc的路由:找不到资源
我正在使用asp.net mvc,但无法访问我的页面,出现404错误 页面的url: 本地主机:2334/RawData/EiphoneNews 查看文件的位置: webroot/View/RawData/TNews/Index.cshtml 我的路线:C# asp.net mvc的路由:找不到资源,c#,asp.net,.net,asp.net-mvc,routes,C#,Asp.net,.net,Asp.net Mvc,Routes,我正在使用asp.net mvc,但无法访问我的页面,出现404错误 页面的url: 本地主机:2334/RawData/EiphoneNews 查看文件的位置: webroot/View/RawData/TNews/Index.cshtml 我的路线: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttr
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"RawData", // Route name
"RawData/{controller}/{action}/{id}", // URL with parameters
new { controller = "EiphoneNews", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "News.Controllers.RawData" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
我的控制器:
namespace News.Controllers.RawData
{
public class EiphoneNewsController : AuthorizedController
{
//
// GET: /EiphoneNews/
public ActionResult Index(int pagenum = 0, int pagesize = 20, string queryString = null)
{...}
}
}
切换路由注册,将采用第一条要匹配的路由,该路由不是原始数据路由。由于路由是在控制器解析之前确定的,因此默认路由导致404而RawData路由不会导致404没有区别
您可能希望通过nuget安装RoutedBugger软件包,以查看路由的运行情况。切换路由注册,将采用第一个要匹配的路由,而不是RawData路由。由于路由是在控制器解析之前确定的,因此默认路由导致404而RawData路由不会导致404没有区别
您可能希望通过nuget安装RoutedBugger软件包,查看路由的情况。交换两个路由定义:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"RawData", // Route name
"RawData/{controller}/{action}/{id}", // URL with parameters
new { controller = "EiphoneNews", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "News.Controllers.RawData" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
交换两个路由定义:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"RawData", // Route name
"RawData/{controller}/{action}/{id}", // URL with parameters
new { controller = "EiphoneNews", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "News.Controllers.RawData" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
切换默认和RawData映射路由。现在它首先检查默认值。切换默认值和RawData MapRoutes。现在它检查默认的first.thx,404错误已经解决,但它找不到视图文件,我的视图文件位置是:webroot/view/RawData/TNews/Index.cshtmlNo,您的视图必须是:~/Views/EiphoneNews/Index.cshtmlc我不能设置一些值让asp.net-mvc搜索路径吗?可以,但为什么要违反标准约定?返回视图时可以指定路径:return view~/view/RawData/TNews/Index.cshtml,someViewModel;。如果此视图必须在某些控制器中使用,您可以将视图放在Views\Sharedthx中,404错误已解决,但它找不到视图文件,我的视图文件位置是:webroot/view/RawData/TNews/Index.cshtmlNo,您的视图必须是:~/Views/EiphoneNews/Index.cshtmlca我可以设置一些值让asp.net-mvc搜索路径吗?您可以,但是你为什么要违反标准惯例呢?返回视图时可以指定路径:return view~/view/RawData/TNews/Index.cshtml,someViewModel;。如果必须在某些控制器中使用此视图,则可以将视图放置在“视图\共享”中