1

我想在任何mvc路由之前註冊一個服務路由。在mvc應用程序中註冊ServiceRoute?

有沒有像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}工作不註冊它的方式。有沒有辦法讓它成爲一個正則表達式約束?

回答

0

我只是寫了粘合劑,這和實現你想要的只是路徑約束:)

嘗試這樣的回答:

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; 
    } 
}
1

感謝您的幫助。我實際上找到了另一種使它工作的方法。 我爲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://kevww.wordpress.com/2012/02/06/implement-dynamic-service-route-for-wcf-just-like-what-you-do-in-mvc/