从 URL 中删除操作名称并将页面标题添加到 URL - Asp.net MVC
Removing Action name from URL and add page title to URL - Asp.net MVC
我有一个 url 这样的:
http://localhost:17594/Contact/Contact
现在我想这样显示:
http://localhost:17594/Contact/Contact-us
路由配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Categories",
url: "Categories/{id}",
defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Contacts",
url: "{controller}/{title}",
defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional , title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
}
查看
@Html.ActionLink("Contact Us", "Contact" , "Contact" , new { title = "contact-us" } , null)
但是我在使用 Categories
地图路线的第 63 行出现错误。
Exception Details: System.InvalidOperationException: No route in the
route table matches the supplied values.
Source Error:
Line 62: @Html.ActionLink("وبلاگ", "")
Line 63: @Html.Action("MenuCat" , "Home")
怎么了?
要将 url 操作显示为联系我们(定义路由),您可以使用属性路由。
[Route("Contact/Contact-us")]
public ActionResult Contact() { … }
有关详细信息,请参阅 msdn。
https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
试试这个
在您的路由配置文件中:
routes.MapRoute(
name: "Contacts",
url: "Contact/{action}/{title}",
defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
你有两个选择。
通过基于约定的路由添加特定路由
routes.MapRoute(
name: "ContactUs",
url: "contact/contact-us",
defaults: new { controller = "Contact", action = "Contact" },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional , title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
或者在基于约定的路由之前在 RouteConfig 中启用属性路由
//enable attribute routing
routes.MapMvcAttributeRoutes();
//other convention-based routes.
routes.MapRoute(....);
并将路由直接应用于控制器和操作。
public class ContactController : Controller {
//GET contact/contact-us
[HttpGet]
[Route("Contact/Contact-us")]
public ActionResult Contact() { … }
}
我有一个 url 这样的:
http://localhost:17594/Contact/Contact
现在我想这样显示:
http://localhost:17594/Contact/Contact-us
路由配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Categories",
url: "Categories/{id}",
defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Contacts",
url: "{controller}/{title}",
defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional , title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
}
查看
@Html.ActionLink("Contact Us", "Contact" , "Contact" , new { title = "contact-us" } , null)
但是我在使用 Categories
地图路线的第 63 行出现错误。
Exception Details: System.InvalidOperationException: No route in the route table matches the supplied values.
Source Error:
Line 62: @Html.ActionLink("وبلاگ", "")
Line 63: @Html.Action("MenuCat" , "Home")
怎么了?
要将 url 操作显示为联系我们(定义路由),您可以使用属性路由。
[Route("Contact/Contact-us")]
public ActionResult Contact() { … }
有关详细信息,请参阅 msdn。 https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
试试这个
在您的路由配置文件中:
routes.MapRoute(
name: "Contacts",
url: "Contact/{action}/{title}",
defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
你有两个选择。
通过基于约定的路由添加特定路由
routes.MapRoute(
name: "ContactUs",
url: "contact/contact-us",
defaults: new { controller = "Contact", action = "Contact" },
namespaces: new[] { "FinalKaminet.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional , title = UrlParameter.Optional },
namespaces: new[] { "FinalKaminet.Controllers" }
);
或者在基于约定的路由之前在 RouteConfig 中启用属性路由
//enable attribute routing
routes.MapMvcAttributeRoutes();
//other convention-based routes.
routes.MapRoute(....);
并将路由直接应用于控制器和操作。
public class ContactController : Controller {
//GET contact/contact-us
[HttpGet]
[Route("Contact/Contact-us")]
public ActionResult Contact() { … }
}