Nik*_*nte 104 c# asp.net-mvc asp.net-mvc-routing
在asp.net MVC中,"主页"(即击中www.foo.com时显示的路线)设置为Home/Index.
我尝试在我的项目中使用Home/Index进行grepping,找不到引用,也无法在IIS(6)中看到任何内容.我查看了root中的default.aspx页面,但这似乎没有做任何相关的事情.
谢谢
Mic*_*tum 149
看看Default.aspx/Default.aspx.cs和Global.asax.cs
您可以设置默认路由:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
只需将控制器/操作名称更改为所需的默认值即可.这应该是路由表中的最后一条路由.
JTW*_*JTW 17
路由Configure在Startup类的方法中配置.要设置"主页",只需添加以下内容即可.当用户导航到您网站的基本URL时,这将导致用户被路由到MapRoute方法中定义的控制器和操作,即,yoursite.com将用户路由到yoursite.com/foo/index:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FooController}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
使用位于App_Start/RouteConfig.cs(MVC 3和4)或Global.asax.cs(MVC 1和2)中的RegisterRoutes方法,如下所示.如果用户导航到您站点的基本URL,这将导致用户被路由到MapRoute方法中定义的控制器和操作,即,yoursite.com将用户路由到yoursite.com/foo/index:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
步骤 1:单击解决方案中的 Global.asax 文件。
第 2 步:然后转到定义
RouteConfig.RegisterRoutes(RouteTable.Routes);
步骤 3:更改控制器名称和视图名称
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)