我想使用一个非常简单的人类可读网址进行路由,如下所示:
我想这样路线:/Controller/Title-Of-The-EF-Object
我不想/Controller/Action/ABCDEF01-2345-6789-0ABC-DEF01234
要么
/Controller/Action/Title-Of-The-EF-Object
。操作由路由处理,因此URL应仅包含控制器和标识符字符串。
我的实体包含一个唯一的,已清理的UrlTitle字符串作为属性:
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" }
);
问题在于,当请求/Controller/Title-Of-The-EF-Object
时,MVC将Title-Of-The-EF-Object
解释为一个动作,因为a)它是一个字符串,而b)它落在该段中,所以我不断得到资源不能是发现。如果我使用实体的Guid而不是UrlTitle,我可以使它工作,因为MVC解释了Guid。
如何为{controller}/{id_string}
创建自定义路线图?
答案 0 :(得分:0)
当我输入问题时,这发生在我身上。路线的顺序错误。 * 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 }
);