[英]issue while making a 301 redirect to the site home page from custom route in an asp.net mvc application
[英]How to redirect page from default route to custom one in MVC application?
我想知道是否有一種方法可以將頁面從“~/ControllerName/Index”重定向到自定義路由,如“~/example-custom”,而無需在 URL 重寫模塊中使用 301 重定向。
我使用了路由屬性:
[Route("example-custom")]
public ActionResult Index()
{
return View();
}
但是有了這個路由屬性“~/ControllerName/Index”就沒有了。 當用戶 go 到“~/ControllerName/Index”時,該路由上不存在頁面。 我想在用戶想要訪問頁面時將用戶重定向到“~/example-custom”。
所以,我想要的是在瀏覽器中顯示鏈接“something.com/example-custom”而不是“something.com/ControllerName/Index”。 但同時訪問“something.com/ControllerName/Index”會自動重定向到“something.com/example-custom”。
將自定義路由添加到RouteConfig.cs
文件中
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "example-custom",
url: "{example-custom}/{id}",
defaults: new { controller = "YourControllerName", action = "YourActionName", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.