简体   繁体   English

如何在命名空间中路由混合 MvcApplication 和 WebApiApplication?

[英]How to Route Mixed MvcApplication and WebApiApplication in a namespace?

I have an MVC application with two controllers, One worked as WebAPiApplication and the second one worked as a regular MvCApplication .我有一个带有两个控制器的 MVC 应用程序,一个用作WebAPiApplication ,第二个用作常规MvCApplication I have configured the route somehow which only the API is accessible.我以某种方式配置了只有 API 可访问的路由。

Can you help me configure it in a way that I can access both controllers ?你能帮我配置一个我可以访问两个控制器的方式吗?

I can access to the API's DefaultController by pointing to http://localhost/api/data .我可以通过指向http://localhost/api/data来访问 API 的DefaultController However, I cannot access to the MvcApplication Index method.但是,我无法访问 MvcApplication Index 方法。 My expectation is to access it by poinitng to http://localhost/Notification but I get error 404 !我的期望是通过指向http://localhost/Notification来访问它,但我收到错误 404 !

Here is my Controllers C# source code:这是我的控制器 C# 源代码:

//DefaultController.cs    
namespace MyApp.Controllers
{
    [RoutePrefix("api")]
    public class DefaultController : ApiController
    {
        [Route("data")]
        [AcceptVerbs("GET")]
        public HttpResponseMessage GetMeData()
        {
             XYZ...
        }
    }
}

//NotificationController.cs    
namespace MyApp.Controllers
{
    public class NotificationController : Controller
    {    
        public ActionResult Index()
        {
            return this.Jsonp(Rep.All());
        }
    }
}

And here how I setup the routes to these two controllers:在这里我如何设置到这两个控制器的路由:

//Global.asax.cs
namespace MyApp
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
            routes.MapRoute(
                "Notification", // Route name
                "{controller}/{action}", // URL with parameters
                new { controller = "Notification", action = "Index" } // Parameter defaults
            );
        }    
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();    
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

//WebApiConfig.cs
namespace MyApp
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();   
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

As mentioned in the comments by @ChimaOsuji ,正如@ChimaOsuji 在评论中提到的,

You've created two applications, WebApiApplication and MvcApplication .您已经创建了两个应用程序, WebApiApplicationMvcApplication You only need one, in which you register routes for both MVC and API controllers.您只需要一个,在其中为 MVC 和 API 控制器注册路由。

Global.asax.cs Global.asax.cs

namespace MyApp
{

    public class WebApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
            routes.MapRoute(
                "Notification", // Route name
                "{controller}/{action}", // URL with parameters
                new { controller = "Notification", action = "Index" } // Parameter defaults
            );
        }    
        protected void Application_Start()
        {
            //Web api config
            GlobalConfiguration.Configure(WebApiConfig.Register);
            //MVC config
            AreaRegistration.RegisterAllAreas();    
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

Just combine your two applications into one:只需将您的两个应用程序合二为一:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // this is the call to register your WebApi routes
        GlobalConfiguration.Configure(WebApiConfig.Register);

        // this is the call to register your MVC routes
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        // register other components...
        AreaRegistration.RegisterAllAreas();    
        RegisterGlobalFilters(GlobalFilters.Filters);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM