【发布时间】:2017-10-02 03:54:45
【问题描述】:
我在当前项目中遇到了一个问题,我想为我的页面显示自定义 URL。我尝试了很多技术,但没有一个能满足我的要求。 我想要这样的网址:
http://www.anyDomain.com/What-Is-Your-Name
目前,我可以这样设置 URL:
http://www.anyDomain.com/What-Is-Your-Name?Id=1
我想忽略来自 URL 的查询字符串。这样控制器就可以识别请求并做出相应的响应。
这里,Id 用于从数据库中获取详细信息。如何将参数值从View 传递到Controller,这样它就可以在不添加 URL 的情况下识别请求?
我的控制器
[Route("~/{CategoryName}")]
public ActionResult PropertyDetails(int Id)
{
}
路由配置
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults:
new
{
controller = "Home",
action = "Index",
}
);
我的观点
<a href="@Url.Action("PropertyDetails", "Home", new {@Id=item.ID,@CategoryName = Item.Title })">
我刚刚注意到,我想要像 StackOverflow 这样的 URL 正在使用
http://stackoverflow.com/questions/43774917/wordpress-blog-type-permalink-in-mvccustom-url-routing
【问题讨论】:
-
我认为这个网址肯定能解决你的问题,请试试这个:jerriepelser.com/blog/generate-seo-friendly-urls-aspnet-mvc
标签: c# asp.net-mvc asp.net-mvc-routing custom-url