从 RouteConfig 或控制器操作方法重定向到命名路由
Redirect to Named Route from RouteConfig or Controller Action Method
我在 ASP.NET MVC 中有以下命名路由:
[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
我的情况是这个应用程序不再有主页,不需要 /Home/Index.cshtml。但是,我不想删除 Index.cshtml 也不想将 Build.cshtml 代码移动到 Index.cshtml
我只是想让默认路由成为我的构建路由。不幸的是,我在整个应用程序中使用了命名的构建路由 [Route("build", Name = "Build")]
,@Html.RouteLink("GET BUILDING", "Build")
正在创建它,所以我无法使用以下任一方法从 http://www.mywebsite.com to http://www.mywebsite.com/build 进行简单的重定向:
在我的RouteConfig中,这个不行,你去http://wwww.mywebsite.com:
找不到资源
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Build", id = UrlParameter.Optional }
);
还有,更奇怪的是,这个也不行,还导致你去http://wwww.mywebsite.com:
找不到资源
public ActionResult Index()
{
return RedirectToRoute("Build");
}
我这样注释掉我命名的路线:
//[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
那么我上面的 RouteConfig 方法就可以了。
我更愿意重定向到我的 RouteConfig 中的命名路由,但如果我可以从我的 /home/index 操作方法重定向到命名路由就很好
RouteLink
采用路由名称,而不是路由 pattern/template,因此您可以简单地将路由模式更改为 ""
(空字符串)
[Route("", Name = "Build")]
public ActionResult Build()
{
return View();
}
当请求到达 yourSiteName.com
时,它将由 Build
操作方法处理,因为它与路由模式匹配(baseurl 之后没有任何内容)
在 RouteLink
辅助方法中使用路由名称的所有现有代码仍然有效,因为我们没有更改它。
@Html.RouteLink("GET BUILDING", "Build")
如果您想让 yourSiteName/Build
也能正常工作,您可以在当前的路由定义中添加另一个路由模式为“”的路由。
[Route("")]
[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
现在,当请求到达 yourSiteName.com
或 yourSiteName.com/build
时,它将由 Build
操作方法
处理
我在 ASP.NET MVC 中有以下命名路由:
[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
我的情况是这个应用程序不再有主页,不需要 /Home/Index.cshtml。但是,我不想删除 Index.cshtml 也不想将 Build.cshtml 代码移动到 Index.cshtml
我只是想让默认路由成为我的构建路由。不幸的是,我在整个应用程序中使用了命名的构建路由 [Route("build", Name = "Build")]
,@Html.RouteLink("GET BUILDING", "Build")
正在创建它,所以我无法使用以下任一方法从 http://www.mywebsite.com to http://www.mywebsite.com/build 进行简单的重定向:
在我的RouteConfig中,这个不行,你去http://wwww.mywebsite.com:
找不到资源routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Build", id = UrlParameter.Optional }
);
还有,更奇怪的是,这个也不行,还导致你去http://wwww.mywebsite.com:
找不到资源public ActionResult Index()
{
return RedirectToRoute("Build");
}
我这样注释掉我命名的路线:
//[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
那么我上面的 RouteConfig 方法就可以了。
我更愿意重定向到我的 RouteConfig 中的命名路由,但如果我可以从我的 /home/index 操作方法重定向到命名路由就很好
RouteLink
采用路由名称,而不是路由 pattern/template,因此您可以简单地将路由模式更改为 ""
(空字符串)
[Route("", Name = "Build")]
public ActionResult Build()
{
return View();
}
当请求到达 yourSiteName.com
时,它将由 Build
操作方法处理,因为它与路由模式匹配(baseurl 之后没有任何内容)
在 RouteLink
辅助方法中使用路由名称的所有现有代码仍然有效,因为我们没有更改它。
@Html.RouteLink("GET BUILDING", "Build")
如果您想让 yourSiteName/Build
也能正常工作,您可以在当前的路由定义中添加另一个路由模式为“”的路由。
[Route("")]
[Route("build", Name = "Build")]
public ActionResult Build()
{
return View();
}
现在,当请求到达 yourSiteName.com
或 yourSiteName.com/build
时,它将由 Build
操作方法