在asp.net MVC中,“首页”(即打到www.foo.com时显示的路径)被设置为Home/Index。在Asp.Net中设置“主页”MVC
- 该值存储在哪里?
- 如何更改“主页”?
- 在家庭控制器的索引操作中,是否比使用RedirectToRoute()更优雅?
我尝试在我的项目中寻找Home/Index,并找不到引用,也不能在IIS(6)中看到任何内容。我查看了根目录下的default.aspx页面,但似乎没有做任何相关的事情。
感谢
在asp.net MVC中,“首页”(即打到www.foo.com时显示的路径)被设置为Home/Index。在Asp.Net中设置“主页”MVC
我尝试在我的项目中寻找Home/Index,并找不到引用,也不能在IIS(6)中看到任何内容。我查看了根目录下的default.aspx页面,但似乎没有做任何相关的事情。
感谢
看那Default.aspx/Default.aspx.cs
和Global.asax.cs中
你可以设置缺省路由
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
只要改变控制器/动作名称到您所需的默认。这应该是路由表中的最后一个路由。
检查的RegisterRoutes方法中的global.asax.cs - 它是路由配置默认的地方......
路由在Startup
类的Configure
方法构成。要设置“主页”,只需添加以下内容。这将导致用户被路由到MapRoute方法中定义的控制器和操作,如果他们导航到您的网站的基本URL,即yoursite.com将路由用户到yoursite.com/foo/index:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FooController}/{action=Index}/{id?}");
});
使用位于App_Start/RouteConfig.cs(MVC 3和4)或Global.asax.cs(MVC 1和2)中的RegisterRoutes方法,如下所示。这将导致用户路由到控制器和动作的图路线法规定,如果他们浏览到您网站的基本URL,即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 }
);
}
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 = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
);
}
}
我试过了答案,但它没有为我工作。这是我最终做的:
创建一个新的控制器DefaultController。在索引操作中,我写了一行重定向:
return Redirect("~/Default.aspx")
在RouteConfig.cs中,更改该路由的controller =“Default”。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
第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 }
);
}
}
MVC之前5通过调用routes.MapRoute(...)
可以将URL映射到特定的操作和控制器在RouteConfig.cs文件中。这是存储主页网址的地方(Home/Index
)。但是,如果修改默认路由,如下所示,请注意,这将影响其他操作和控制器的URL。例如,如果您有一个名为ExampleController
的控制器类和其中的一个操作方法,名为DoSomething
,则预期默认网址ExampleController/DoSomething
将不再工作,因为默认路由已更改。
对此的解决方法是不使用默认的路由很乱,创造了其他动作和控制器,像这样的RouteConfig.cs文件新航线,
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Example",
url: "hey/now",
defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);
现在ExampleController
类的DoSomething
行动将被映射到网址hey/now
。但是,每次你想为不同的动作定义路线时,这可能会很乏味。因此,在MVC 5您现在可以添加的属性相匹配的网址,像这样的行动,
public class HomeController : Controller
{
// url is now 'index/' instead of 'home/index'
[Route("index")]
public ActionResult Index()
{
return View();
}
// url is now 'create/new' instead of 'home/create'
[Route("create/new")]
public ActionResult Create()
{
return View();
}
}
在MVC 4,这可以在App_Start/RouteConfig.cs – NikolaiDante 2012-09-04 15:54:25
@NikolaiDante做你应该做的是评论一个答案,我几乎错过了它,它比这个答案更快。 :)谢谢 – GazB 2013-05-21 12:20:53