Hosting a WCF Service in an MVC application outside of Areas

梦想与她 提交于 2019-12-05 13:02:41

I've figured out the problem. First of all, I was missing a part of the path to the function that registers my routes. After fixing that path I was able to get my wsdl showing up in my hosting environment. However, this messed up the default routing for my areas. So for anyone who has this problem in the future, here is my solution:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.svc/{*pathInfo}");

        routes.MapRoute(
            "CustomFunctions", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new
            {
                controller = "CustomFunctions",
                action = "Index",
                id = UrlParameter.Optional
            }, // Parameter defaults
            new { controller = "^(?!CustomFunctions).*" }
        );

        routes.Add(new ServiceRoute("CustomFunctions", new ServiceHostFactory(),
                   typeof(CustomFunctions)));

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");           

        // Had to do some special stuff here to get this to work using a default area and no controllers/view in the root
        routes.MapRoute(
            name: "Default",
            url: "{area}/{controller}/{action}/{id}",
            defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "Viper.Areas.Home" }
        ).DataTokens.Add("area", "Home");           
    }

I specified my custom route primarily so that when I navigated to the specified url then it would show my .svc file. I call this method from my ApplicationStart method in Global.asax.cs. I also had to create a seperate controller and view for my CustomFunctions within my Home Area so that it could differentiate between my default route and my CustomFunctions, and specified that in my route map as seen above. So when I go to localhost\Viper it will find the route specified in my default map and when I go to localhost\Viper\CustomFunctions it will find the route to my .svc file. The IgnoreRoute basically makes it so you don't have to put the file extension at the end of the url when calling your page. So rather than CustomFunctions.svc I only specify CustomFunctions. Make sure you add the System.ServiceModel.Activation assembly and using statement to your project when doing this.

Thanks everyone for your help. Hope this helps some other poor lost soul.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!