My web API is working well but when i write same code in area folder it not working.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "FeatureA",
routeTemplate: "FeatureA/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
You need to put your routing details in the "YourHubNameAreaRegistration" class that's in your Area folder. It goes in the RegisterArea method and should look something like this:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Hub_default",
"api/Hub/{action}/{id}",
new { controller = "Hub", action = "Index", id = UrlParameter.Optional }
);
}
Related
The structure of webapi is as follows:
App
-Area
-MyArea1
- ControllerA (Route('api/myarea1/controllera'))
-MyArea2
- ControllerA (Route('api/myarea2/controllera'))
Problem is that the route api/myarea1/controllera and api/myarea2/controllera are not being resolved. It comes are 404.
I read somewhere we need to implement IHttpControllerSelector but not sure what is the simplest way to implement this. If there is any other way it can be done?
Any idea.
Edit:
RouteConfig.cs
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 }
);
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Edit 2:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapHttpRoute(
name : "MyArea1_default",
routeTemplate : "MyArea1{controller}/{action}/{id}",
defaults : new { action = "Index", id = UrlParameter.Optional }
);
}
If you want to use controllers with the same name, you need to specify the namespace for each route configuration.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapHttpRoute(
name : "MyArea1_default",
routeTemplate : "MyArea1{controller}/{action}/{id}",
defaults : new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Your.Controller.Namespace.Here" }
);
}
For your reference:
http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx/
This is my webApiConfig
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute(
name: "ApiByActionWithid",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "" },
constraints: new { id = #"^[0-9]+$" });
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "" });
The changeOrderState action looks like this:
public ResponseDTO ChangeOrderState(Int32 changeOrderState, string state)
{
IRestaurantManager rm = Core.CoreManager.Instance.GetService<IRestaurantManager>();
return rm.ChangeOrderState(changeOrderState, state);
}
And I call a method like this:
https://localhost:44302/api/Restaurant?changeOrderState=1&state=Rechazado
But it doesn't work, but in others calls that the same way do work
i solved this, the answer is add the next in te method [HttpGet] and my action looks like this:
[HttpGet]
public ResponseDTO ChangeOrderState(int changeOrderState, string state="")
{
IRestaurantManager rm = Core.CoreManager.Instance.GetService<IRestaurantManager>();
return rm.ChangeOrderState(changeOrderState, state);
}
I have a Web Api controller which returns 404 when I call it.
public class ValuesController : ApiController
{
[HttpGet]
public static string Test()
{
return "Hola!";
}
}
Heres the Route Config
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And here's the Route Debugger info.
I get 404 for below requests
http://localhost:8081/api/values/test
http://localhost:8081/api/values/get
Any ideas why its failing?
Your action is defined as a static method. Actions cannot be static.
[HttpGet]
public static string Test()
{
return "Hola!";
}
Make it an instance method and your code will work.
[HttpGet]
public string Test()
{
return "Hola!";
}
How can I simplify these routes?
config.Routes.MapHttpRoute(
name: "GetUsersRoute",
routeTemplate: "api/User",
defaults: new { controller = "User", action = "GetUsers" }
);
config.Routes.MapHttpRoute(
name: "GetUserRoute",
routeTemplate: "api/User/{id}",
defaults: new { controller = "User", action = "GetUser", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "GetEmailAddressRoute",
routeTemplate: "api/User/GetEmailAddress/{id}",
defaults: new { controller = "User", action="GetEmailAddress", id = RouteParameter.Optional }
);
/*** Default ***/
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I needed to add the GetEmailAddressRoute because the controller didn't know which action to take for a path like api/User/5. However, since then former routes like api/User don't lead to the correct action.
Some action signatures from the controller:
public Object GetUsers() { };
public Object GetUser(int id) { };
public HttpResponseMessage GetEmailAddress (int id) { };
But now I'd need to add another route for the Post actions. Is there a simpler way?
Update: What helped me for posting new users was to remove the action from the first route. Now it is
config.Routes.MapHttpRoute(
name: "GetUsersRoute",
routeTemplate: "api/User",
defaults: new { controller = "User" }
);
You can take advantage of the new routing attribute feature coming with web api 2 :
[Route("api/User")]
public Object GetUsers() { };
[Route("api/User/{id}")]
public Object GetUser(int id) { };
[Route("api/User/GetEmailAddress/{id}")]
public HttpResponseMessage GetEmailAddress (int id) { };
I have created a WebApi project, which has multiple WebApi controllers. As per my requirement I have to add an MVC controller to my WebApi project. My client side code is making an ajax request to that MVC controller with {ControllerName/ActionMethodName}. I have multiple routes in my global.asax file:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "ActionRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Below is my ReportServiceBaseController:
protected virtual ReportServiceBase CreateReportService();
[HttpPost]
public JsonResult LoadDocumentInfo(LoadDocumentInfoRequest request);
[HttpPost]
public JsonResult LoadDocumentMapInfo(LoadDocumentMapInfoRequest request);
[HttpPost]
public JsonResult LoadDocumentMapInfoFull(LoadDocumentMapInfoFullRequest request);
[HttpPost]
public JsonResult LoadPageInfo(LoadPageInfoRequest request);
I am inheriting that controller in a ReportService controller:
public class ReportServiceController : ReportServiceBaseController
{
protected override PerpetuumSoft.Reporting.WebViewer.Server.ReportServiceBase CreateReportService()
{
return new ServiceClass();
}
}
The client side URL which is making the request:
"http://" + hostName + "/ReportService/LoadDocumentInfo"
I think the WebApi route is suppressing the MVC Controller route. How can I separate the MVC Controller route from the WebApi route?
You can create a new config class of WebApi, ApiRouteConfig.cs, within App_Start.
namespace Mvc4App
{
public class ApiRouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "ActionRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Then, edit Global.asax.cs, to call ApiRouteConfig.RegisterRoutes(RouteTable.Routes);