如何创建类型的System.Guid的路由约束? [英] How can I create a route constraint of type System.Guid?
本文介绍了如何创建类型的System.Guid的路由约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以点我在正确的方向上如何映射它需要两个GUID的路线?
Can anyone point me in the right direction on how to map a route which requires two guids?
IE浏览器。 <一href=\"http://blah.com/somecontroller/someaction/\">http://blah.com/somecontroller/someaction/{firstGuid}/{secondGuid}
ie. http://blah.com/somecontroller/someaction/{firstGuid}/{secondGuid}
其中两个firstGuid和secondGuid不是可选的,必须是类型的System.Guid的?
where both firstGuid and secondGuid are not optional and must be of type system.Guid?
推荐答案
创建RouteConstraint如下所示:
Create a RouteConstraint like the following:
public class GuidConstraint : IRouteConstraint {
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (values.ContainsKey(parameterName))
{
string stringValue = values[parameterName] as string;
if (!string.IsNullOrEmpty(stringValue))
{
Guid guidValue;
return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty);
}
}
return false;
}}
接下来添加路由时:
Next when adding the route :
routes.MapRoute("doubleGuid", "{controller}/{action}/{guid1}/{guid2}", new { controller = "YourController", action = "YourAction" }, new { guid1 = new GuidConstraint(), guid2 = new GuidConstraint() });
这篇关于如何创建类型的System.Guid的路由约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文