我正在尝试在任何mvc路由之前注册服务路由。
有没有像product/296eb068-2a1a-439a-b608-6dc0da49cb36一样注册它的方法?
var factory = new DataServiceHostFactory();
var serviceRoute = new ServiceRoute("product/{*guid only}", factory,
typeof(ProductService));
serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" }};
serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata"}};
routes.Add("myproduct", serviceRoute);我知道{*guid only}不工作了。有没有办法使它成为正则表达式约束??
发布于 2012-02-05 10:03:46
我刚刚为此编写了一个活页夹,并意识到您只需要路由约束:)
试试这个答案:
How can I create a route constraint of type System.Guid?
看一下Olivehour的答案:
public class NonEmptyGuidRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route,
string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (values.ContainsKey(parameterName))
{
var guid = values[parameterName] as Guid?;
if (!guid.HasValue)
{
var stringValue = values[parameterName] as string;
if (!string.IsNullOrWhiteSpace(stringValue))
{
Guid parsedGuid;
Guid.TryParse(stringValue, out parsedGuid);
guid = parsedGuid;
}
}
return (guid.HasValue && guid.Value != Guid.Empty);
}
return false;
}
}发布于 2012-02-06 08:36:19
谢谢你的帮助。实际上,我找到了另一种方法来让它工作。我为ServiceRoute创建了一个DynamicServiceRoute类,它允许您将动态路由映射到单个服务。
public class DynamicServiceRoute
: RouteBase, IRouteHandler
{
private string virtualPath = null;
private ServiceRoute innerServiceRoute = null;
private Route innerRoute = null;
public static RouteData GetCurrentRouteData()
{
if (HttpContext.Current != null)
{
var wrapper = new HttpContextWrapper(HttpContext.Current);
return wrapper.Request.RequestContext.RouteData;
}
return null;
}
public DynamicServiceRoute(string pathPrefix, object defaults, ServiceHostFactoryBase serviceHostFactory, Type serviceType)
{
if (pathPrefix.IndexOf("{*") >= 0)
{
throw new ArgumentException("Path prefix can not include catch-all route parameters.", "pathPrefix");
}
if (!pathPrefix.EndsWith("/"))
{
pathPrefix += "/";
}
pathPrefix += "{*servicePath}";
virtualPath = serviceType.FullName + "-" + Guid.NewGuid().ToString() + "/";
innerServiceRoute = new ServiceRoute(virtualPath, serviceHostFactory, serviceType);
innerRoute = new Route(pathPrefix, new RouteValueDictionary(defaults), this);
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
return innerRoute.GetRouteData(httpContext);
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.HttpContext.RewritePath("~/" + virtualPath + requestContext.RouteData.Values["servicePath"], true);
return innerServiceRoute.RouteHandler.GetHttpHandler(requestContext);
}
}然后,您可以在global.asax文件中注册路径
var factory = new DataServiceHostFactory();
RouteTable.Routes.Add(new DynamicServiceRoute("nuget/{customername}", null, factory, typeof(Packages)));这是我的一篇博客文章,可以阅读更多关于它的信息。干杯
https://stackoverflow.com/questions/9146010
复制相似问题