MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links!

后端 未结 3 1369
天命终不由人
天命终不由人 2020-12-05 07:02

I have a WCF service that lives side-by-side with an MVC2 web site. I\'d like for my URL for the service to look like this:

http://localhost/projdir/Service

相关标签:
3条回答
  • 2020-12-05 07:37

    Try moving the Service route after the MVC route. But to avoid the "missing controller" error that you got before, add the MVC route with a Route Constraint. These route constraints can be Regex - basically you'd want your route constraint to be any controller that is not "Service". When a request for "Service" is requested, it will make it fall through and his the WCF Service Route.

    0 讨论(0)
  • 2020-12-05 07:38

    Another solution is to inherit the ServiceRoute and override the GetVirtualPath method to return null as described here

    public class AppServiceRoute : ServiceRoute
    {
        public AppServiceRoute(string routePrefix, ServiceHostFactoryBase serviceHostFactory, Type serviceType)
            : base(routePrefix, serviceHostFactory, serviceType)
        {
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null;
        }
    }
    

    This way, reverse route mapping never select this route for any Action. Works like a charm

    0 讨论(0)
  • 2020-12-05 07:58

    I resolved with that:

         routes.MapRoute(
                 "Default", // Route name
                 "{controller}/{action}/{id}", // URL with parameters
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                 new { controller = "^(?!api).*" }
            );
            routes.Add(new ServiceRoute("api", new DataServiceHostFactory(), typeof(dwService)));
    

    I hope this good for you

    0 讨论(0)
提交回复
热议问题