Specify the default controller / action route in the WebAPI using AttributeRouting

How to set the default controller when using AttributeRouting instead of the standard RouteConfiguration that uses WebAPI. i.e. get rid of the comment part of the code, since this is redundant when using AttribteRouting

public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //routes.MapRoute( // name: "Default", // url: "{controller}/{action}/{id}", // defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } //); } } 

If I comment on the section above and try to start the webapi application, I get the following error because the main controller / action is not installed by default. HTTP Error 403.14 - Forbidden The web server is configured to not display the contents of this directory.

How can I specify a route through attribute routing for the Home controller / action?

EDIT: Code Example:

  public class HomeController : Controller { [GET("")] public ActionResult Index() { return View(); } public ActionResult Help() { var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer(); return View(new ApiModel(explorer)); } } 
+6
source share
5 answers

Have you tried the following:

 //[RoutePrefix("")] public class HomeController : Controller { [GET("")] public ActionResult Index() { return View(); } } 

This will add the route to the collection with the url template as ", and the default values ​​for the controller and actions will be" Main "and" Index "respectively.

+5
source

You need to install AttributeRouting (ASP.NET MVC) next to the AttributeRouting (ASP.NET Web API) that you already installed.

MVC and Web API packages are free packages. Both are dependent on AttributeRouting.Core packages. *. AR for MVC - for routes by controller methods; AR for the Web API is designed for routes using the ApiController methods.

+1
source

It worked for me. Add this to Register() in the WebApiConfig class

 config.Routes.MapHttpRoute( name: "AppLaunch", routeTemplate: "", defaults: new { controller = "Home", action = "Get" } ); 
+1
source

Include attribute routing in your WebApiConfig class located in the App_Start folder by placing the following code:

 using System.Web.Http; namespace MarwakoService { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); // Other Web API configuration not shown. } } } 

You can combine with an attribute based on convention:

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

Now go to your global.asax class and add the following line of code:

 GlobalConfiguration.Configure(WebApiConfig.Register); 

Your HomeController is an MVC controller. Try creating an API controller (say CustomerController) and add route attributes as follows:

 [RoutePrefix("api/Product")] public class CustomersController : ApiController { [Route("{customerName}/{customerID:int}/GetCreditCustomer")] public IEnumerable<uspSelectCreditCustomer> GetCreditCustomer(string customerName, int customerID) { ... } 

Hope this helps

+1
source

A standard MVC application can support both MVC and Webapi if you test MVC and Webapi from the Create New Asp.net web application template.

You will need to do the following in RouteConfig.cs, since your request applies only to MVC routes, not webapi:

 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } } 

Pay attention to the line with the parameters "routes.MapMvcAttributeRoutes ();" which provides attribute routes, while a call to "routes.MapRoute (...)" should provide you with the usual route to the default controller and action.

This is because attribute routing and normal routing can be mixed. Hope this helps.

0
source

Source: https://habr.com/ru/post/943828/


All Articles