简体   繁体   English

在Asp.net MVC Razor中将包含GUID的URl重写为查询字符串

[英]Rewriting URl containg GUID as query string in Asp.net MVC Razor

i am working on Asp net MVC razor web application. 我正在研究Asp net MVC剃刀Web应用程序。 i am trying to implement URL rewriting. 我正在尝试实现URL重写。

My Urls are like 我的乌尔斯就像

/User/CategoryDetail?cid=2&lid=b20319e0-55b1-4233-8c63-c35b98c79a84

but i want it to look like 但我希望它看起来像

/User/CategoryDetail/[CategoryName]OR[LocationName]

Can any once suggest for the solution to implement this. 可以一次提出解决方案来建议实现这一点。 Thanks in advance. 提前致谢。

Satpal suggestion is work in case the CategoryName is an optional and LocationName is a mandatory field. 如果CategoryName是可选字段,而LocationName是必填字段,则可以使用Satpal建议。 In case that both fields are optional, then it doesn't work. 如果两个字段都是可选的,则它不起作用。 Because the routing cannot determine which property is provided. 因为路由无法确定提供哪个属性。 In such case, you have two options. 在这种情况下,您有两个选择。 One option is defining 3 different routes for different combination of parameters plus IRouteConstraints, 一种选择是为3种不同的路由定义不同的参数组合以及IRouteConstraints,

routes.MapRoute(name: "CategoryDetail",
url: "/{controller}/{CategoryDetail}/{LocationName}",
defaults: new { controller = "User", action = "Login" },
constraints: {LocationName=@"^\d+$"}
);   

routes.MapRoute(name: "CategoryDetail",
url: "/{controller}/{CategoryDetail}/{CategoryName}",
defaults: new { controller = "User", action = "Login" },
constraints: {CategoryName= new GuidConstraint() }
);   

routes.MapRoute(name: "CategoryDetail",
url: "/{controller}/{CategoryDetail}/{LocationName}/{CategoryName}",
defaults: new { controller = "User", action = "Login", CategoryName = UrlParameter.Optional }
);   

for GuidConstraint please visit here . 对于GuidConstraint,请访问这里 or if you don't like several routes, then uses the approach provided here 或者如果您不喜欢多条路线,请使用此处提供的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM