Asp.net mvc 3 在操作段中使用字符串标识符映射MVC3路由
我想使用一个非常简单的人类可读url进行路由,如下所示: 我想这样路由:Asp.net mvc 3 在操作段中使用字符串标识符映射MVC3路由,asp.net-mvc-3,entity-framework,routing,asp.net-mvc-routing,Asp.net Mvc 3,Entity Framework,Routing,Asp.net Mvc Routing,我想使用一个非常简单的人类可读url进行路由,如下所示: 我想这样路由:/Controller/EF对象的标题 我不想要/Controller/Action/ABCDEF01-2345-6789-0ABC-DEF01234 或 /Controller/Action/EF对象的标题。操作由路由处理,因此url应仅包括控制器和标识符字符串 “我的实体”包含一个唯一的、经过清理的URL标题字符串作为属性: public class Entity { [Key] public Guid
/Controller/EF对象的标题
我不想要/Controller/Action/ABCDEF01-2345-6789-0ABC-DEF01234
或
/Controller/Action/EF对象的标题
。操作由路由处理,因此url应仅包括控制器和标识符字符串
“我的实体”包含一个唯一的、经过清理的URL标题字符串作为属性:
public class Entity {
[Key]
public Guid ID { get; set; }
//Example: "Title of the EF object"
public string Title { get; set; }
//Example: "Title-Of-The-EF-Object"
public string UrlTitle { set; set; }
}
当前路线图:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Item",
"{controller}/{UrlTitle}",
new { controller = "{controller}", action = "Details" }
);
问题是,当请求EF对象的/Controller/Title
时,MVC将EF对象的Title解释为一个操作,因为a)它是一个字符串,b)它在段中的位置,所以我一直无法找到资源。如果我使用实体的Guid而不是UrlTitle,我可以让它工作,因为MVC将Guid解释为这样
如何为{controller}/{id_string}
创建自定义路线图?我在键入问题时想到了这一点。路线顺序不对*0_0*
routes.MapRoute(
"Item",
"{controller}/{UrlTitle}",
new { controller = "{controller}", action = "Details" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);