路由的要求不是空参数到控制器 [英] Routing to controller with Required not empty parameter
本文介绍了路由的要求不是空参数到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我沃尔德样,来图的http://本地主机/ GUID的推移,这里 ResellerController <该控制器/ code>和消防
首页
动作只有当 GUID的推移,这里
为不为空 GUID。我的路由表看起来如下:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
经销商,
{ID},
新{控制器=经销商,行动=索引,ID = Guid.Empty}
//我们可以标记参数UrlParameter.Optional,但如何让需要它?
); routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
); }
和我的行动看起来像这样:
公众的ActionResult指数(GUID ID)
{
//做一些东西在这里
}
但经过应用程序启动(与的http://本地主机)重定向我 ResellerController
空GUID作为 ID
参数。
解决方案
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
经销商,
{ID},
新{控制器=经销商,行动=索引,ID = UrlParameter.Optional},
新{ID = @\"^(\\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\\}{0,1})$\" }
); routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
);
}
或者如果你想要一个更强大的约束比一些神秘的正则表达式:
公共类GuidConstraint:IRouteConstraint
{
公共BOOL匹配(HttpContextBase HttpContext的,路由路径,字符串参数名称,RouteValueDictionary价值,RouteDirection routeDirection)
{
VAR值=值[参数名称]作为字符串;
GUID GUID;
如果(string.IsNullOrEmpty(值)及!&安培; Guid.TryParse(价值,走出GUID))
{
返回true;
}
返回false;
}
}
和则:
routes.MapRoute(
经销商,
{ID},
新{控制器=经销商,行动=索引,ID = UrlParameter.Optional},
新{ID =新GuidConstraint()}
);
I wold like to map http://localhost/Guid-goes-here to ResellerController
and fire Index
action of that controller only when Guid-goes-here
is not empty Guid. My routing table looks as follow:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Reseller",
"{id}",
new { controller = "Reseller", action = "Index", id = Guid.Empty }
//We can mark parameters as UrlParameter.Optional, but how to make it required?
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
And my action looks like this:
public ActionResult Index(Guid id)
{
// do some stuff here
}
But after application is started (with http://localhost) it redirects me to ResellerController
with empty Guid as id
parameter.
解决方案
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Reseller",
"{id}",
new { controller = "Reseller", action = "Index", id = UrlParameter.Optional },
new { id = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
or if you want a more robust constraint than some cryptic regex:
public class GuidConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var value = values[parameterName] as string;
Guid guid;
if (!string.IsNullOrEmpty(value) && Guid.TryParse(value, out guid))
{
return true;
}
return false;
}
}
and then:
routes.MapRoute(
"Reseller",
"{id}",
new { controller = "Reseller", action = "Index", id = UrlParameter.Optional },
new { id = new GuidConstraint() }
);
这篇关于路由的要求不是空参数到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文