简体   繁体   中英

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 . I have configured the route somehow which only the API is accessible.

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 . However, I cannot access to the MvcApplication Index method. My expectation is to access it by poinitng to http://localhost/Notification but I get error 404 !

Here is my Controllers C# source code:

//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 ,

You've created two applications, WebApiApplication and MvcApplication . You only need one, in which you register routes for both MVC and API controllers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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