I have an ASP .NET MVC application with Angular 2. I added a WebApi Controller and try to communicate from angular service with it, but it does not access an action within the WebApi Controller with two parameters.
I think I read all the question regarding that matter. I have my webApi routes registered before the MVC ones. I can access the action, if it does not require any parameters. But if I try to hit it with the parameters I keep getting 404 not found for the Login action - POST localhost:12312/api/userapi/login 404 (Not Found).
I tried every kind of formatting for the WebApiConfig - with optional parameters, without them, with dynamic controller placeholders, with the exact names for action and controller,etc. I tried Routing attribute as well, but with no effect.
I tried to put it as a single parameter "object credentials" and modified the WebApiConfig.cs as well, but the result didn't change.
RouteConfig.cs
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{*anything}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
WebApiConfig.cs
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "api",
routeTemplate: "api/userapi/login/{username}/{password}",
defaults: new { username = RouteParameter.Optional, password =
RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
UserApiController.cs
[HttpPost]
public HttpResponseMessage LogIn(string username, string password)
{
return ToJson(userRep.LogIn(username, password));
}
angular
this.http.post(url, JSON.stringify(credentials), { headers: headers })
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
Am not sure why its not working in your way(using parmas) but i will simply create an model something like
public class LoginModel{
public UserName {get;set;}
public Password {get;set;}
}
then in login mathod
[HttpPost]
public HttpResponseMessage LogIn(LoginModel model)
{
return ToJson(userRep.LogIn(model.username, model.password));
}
No need of custom routing just use default config
Related
Did a lot of reading around this but no existing fixes work for me.
I am trying to hit an api endpoint from a apiClient project but I keep getting the error message: No action was found on the controller 'UserApi' that matches the request.
I'm able to debug into the api controller but it just won't hit the method.
Client:
public async Task<bool> UserExists(UserDto dto)
{
var postUrl = $"{BaseUri}UserApi/user-exists";
var json = await PostAsync(new Uri(postUrl), dto);
return JsonConvert.DeserializeObject<bool>(json);
}
Api controller:
[Route("api/UserApi")]
public class UserApiController : ApiController
{
public UserApiController()
{
}
[HttpPost]
[Route("user-exists")]
public async Task<bool> UserExists([FromBody]UserDto dto)
{
return true;
}
Route config:
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 }
);
routes.MapHttpRoute(
name: "ApiAction",
routeTemplate: "api/{controller}/{action}/{dto}",
defaults: new { dto = UrlParameter.Optional }
);
}
The bottom routing configuration is the one I'm trying to use. Any help is appreciated
the first comment on the original post solved half my issue - I was trying to configure routing two different ways. I removed all my code from RegisterRoutes and used
config.MapHttpAttributeRoutes();
in WebApiConfig.
I also needed to use the RoutePrefix attribute on the api controller instead of just Route - which is to be used on the controller methods. working now
1.In Global.asax.cs just comment
//RouteConfig.RegisterRoutes(RouteTable.Routes);
As you are using web api no need of this route configuration
2.In WebApiConfig.cs use below line only
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I'm fairly new to WepApi2 and creating restfull webservice, but I get the hang of most of it.
Recently, I started developing a new WebApi2 where I encounter that some of my queries required some unesacapable characters, specially the slash and inverted slash ones.
I read several tutorials and questions here about the matter, but none would suffice me, so I ended up to setting my api queries like this
http://host/controler/action/?param1=x¶m2=y
Everything works perfect. I had to setup the ActionName and Route attributes to my action, no problem there, but once I tried the old way of query
http://host/controller/action/x/y
I would always get a No action was found on the controller 'Controller' that matches the request.
Here's my routes configuration
// Web API configuration and services
config.MapHttpAttributeRoutes();
// Web API routes
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { controller = "HelloWorld", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Config1",
routeTemplate: "{controller}/{action}/{param1}/{param2}/{param3}",
defaults: new { controller = "Controller", action = "action", param2 = RouteParameter.Optional, param3 = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Config2",
routeTemplate: "{controller}/{param1}/{param2}/{param3}",
defaults: new { controller = "action", param3 = RouteParameter.Optional }
);
You may notice I have 2 configurations. Well, the first is to classify the action under a controller, as so, in the future, can add more controllers with specific actions, instead of having a huge list of controllers with an specific action, but the second approach is what the client wants.
So, is there a way to have both ways working together??
Edit: My bad, I forgot to show how my controller and actions where setup
public class ControllerController : WebApiController
{
[Route("Controller/action1/")]
[ActionName("action1")]
[HttpGet()]
public Object action1(string param1, string param2)
{
// do action actions code
return result;
}
As you can see, besides setting the route in the config, I'm also setting the action routing attribute, but I wasn't aware I could set more than 1 Route attribute. Will try that and report back!
You could try using attribute routing instead:
// in your startup configuration:
config.MapHttpAttributeRoutes();
// and your controller:
[RoutePrefix("foo")]
public class FooController
{
[HttpGet]
[Route("bar/{param1}/{param2}")]
[Route("bar")
public IHttpActionResult GetBar(string param1, string param2)
{
// ...
}
}
I have two end points:
api/v1/user/session (For creating user login with post request )
api/v1/user (For creating user with post request)
How to route this two endpoints in same controller? I also want to specify action for a specific request. More clearly:
all get,post,update, patch operations can be done in api/v1/user/session endpoint
all get,post,update, patch operations can be done in api/v1/user endpoint
Is it possible ?
Example:
config.Routes.MapHttpRoute(
"UserApi",
"api/v1/{controller}/session",
new { controller = "User", action="Session" });
Now, I want all rest requests to work for Session method with [httpPost],[httpGet] etc attributes.
config.Routes.MapHttpRoute("lol", "api/v1/{controller}/session",
new { controller = "User", action="Session" });
//config.Routes.MapHttpRoute(
// name: "LoginApi",
// routeTemplate: "api/v1/{controller}",
// defaults: new { controller = "User"}
//);
config.Routes.MapHttpRoute(
name: "RailStationApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I would suggest you look at attribute routing - this is a lot easier to specify than using the central configuration.
[RoutePrefix("api/v1")]
public class UserController : ApiController {
[HttpPost]
[Route("user/session")]
public void Login(/*...*/) {
// ...
}
[HttpGet]
[Route("user/session")] // Note this has the same route as Login
public SessionResult GetSession(/*...*/) {
// ...
}
[HttpPost]
[Route("user")]
public void CreateUser(/*...*/) {
// ...
}
}
Note that you don't technically need [HttpPost] since it is the default, but I included it for clarity. You can add methods with the other Http verbs in the same way.
I tried with hardcoded solution and it worked. I added following route into the webapi.config file and it worked.
RouteTable.Routes.MapHttpRoute(
name: "SessionApi",
routeTemplate: "api/v1/user/session",
defaults: new { Controller = "Session", id = RouteParameter.Optional }
).RouteHandler = new SessionStateRouteHandler();
I have these two routes defined:
routes.MapRoute(
name: "GetVoucherTypesForPartner",
url: "api/Partner/{partnerId}/VoucherType",
defaults: new { controller = "Partner", action = "GetVoucherTypesForPartner"}
);
routes.MapRoute(
name: "Default",
url: "api/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
In my PartnerProfile controller, I have 2 methods:
public Partner Get(string id)
{ }
public IEnumerable<string> GetVoucherTypesForPartner(string id)
{ }
If I hit the url ~/api/Partner/1234 then, as expected, the Get method is called.
However, if I hit the url ~/api/Partner/1234/VoucherType then the same Get method is called. I am expecting my GetVoucherTypesForPartner to be called instead.
I'm pretty sure something in my route setup is wrong...
You seem to have mapped standard MVC routes, not Web API routes. There's a big difference. The standard routes are used by controllers deriving from the Controller class, but if you are using the ASP.NET Web API and your controllers are deriving from the ApiController type then you should define HTTP routes.
You should do that in your ~/App_Start/WebApiConfig.cs and not inside your ~/App_Start/RouteConfig.cs.
So go ahead:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "GetVoucherTypesForPartner",
routeTemplate: "api/Partner/{partnerId}/VoucherType",
defaults: new { controller = "Partner", action = "GetVoucherTypesForPartner" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
and then:
public class PartnerController : ApiController
{
public Partner Get(string id)
{
...
}
public IEnumerable<string> GetVoucherTypesForPartner(string partnerId)
{
...
}
}
Things to notice:
We have defined HTTP routes not standard MVC routes
The parameter that the GetVoucherTypesForPartner action takes must be called partnerId instead of id in order to respect your route definition and avoid any confusions
I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
The problem occure when i added the action GetFilteredCeremonies to my controller. After adding this when i make an ajax call to GetCeremonies action then it return an Exception with following message :
"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were
found that match the request
FYI: The parameter Search is the Model class which contains properties and a function name Ceremonies.
EDIT
Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you're not requirement bound to use REST services that use api/{controller}/{id} route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id} and it will solve your problems.
The problem here is your 2 Get methods will resolve to api/Ceremony and MVC does not allow parameter overloading. A quick workaround (not necessarily the preferred approach) for this sort of problem is to make your id parameter nullable e.g.
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies(int? id)
{
if (id.HasValue)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
else
{
return db.Ceremonies.AsEnumerable();
}
}
However, you would then be returning a list of ceremonies when with 1 item when your trying to query for a single ceremony - if you could live with that then it may be the solution for you.
The recommended solution is to map your paths appropriately to the correct actions e.g.
context.Routes.MapHttpRoute(
name: "GetAllCeremonies",
routeTemplate: "api/{controller}",
defaults: new { action = "GetCeremonies" }
);
context.Routes.MapHttpRoute(
name: "GetSingleCeremony",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GetCeremony", id = UrlParameter.Optional }
);
Luckily nowadays with WEB API2 you can use Attribute Routing. Microsoft has gone open source on a big scale and then a wizard named Tim McCall contributed it from the community. So since somewhere end 2013, early 2014 you can add attributes like [Route("myroute")] on your WEB API methods. See below code example.
Still - as I just found out - you have to make sure to use System.Web.Http.Route and NOT System.Web.Mvc.Route. Otherwise you'll still get the error message Multiple actions were found that match the request.
using System.Web.Http;
...
[Route("getceremonies")]
[HttpGet]
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
[Route("getceremony")]
[HttpGet]
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
[Route("getfilteredceremonies")]
[HttpGet]
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
Here is my controller:
public class PhoneFeaturesController : ApiController
{
public List<PhoneFeature> GetbyPhoneId(int id)
{
var repository = new PhoneFeatureRepository();
return repository.GetFeaturesByPhoneId(id);
}
public PhoneFeature GetByFeatureId(int id)
{
var repository = new PhoneFeatureRepository();
return repository.GetFeaturesById(id);
}
}
Here is my api routing:
config.Routes.MapHttpRoute(
name: "ApiWithId",
routeTemplate: "Api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = #"^[0-9]+$" });
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null,
constraints: new { name = #"^[a-z]+$" });
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Get" },
constraints: new { id = #"^[0-9]+$" });
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I tested it like this:
/api/PhoneFeatures//api/PhoneFeatures/GetFeatureById/101
/api/PhoneFeatures/GetByFeatureId/12
It works smooth in every condition :)
I found another fix that doesn't require moving methods out of the controller or changing the route mapping config. Just add the [NonAction] attribute to the method you want to exclude:
[NonAction]
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
Please check you have two methods which has the different name and same parameters.
If so please delete any of the method and try.
This error was raised because there are two methods which are looking for same parameters. try to delete any one of them and try...
I hope you are doing HttpGet while you invoke GetFilteredCeremonies(Search filter)
In that case, you cannot pass complex object in GET request like Search that you are passing.
If for some reason, you definitely want to get complex types in your get request, there are some work around. You may need to write a custom model binder and then set the attribute. please refer this article.
Edit Your WebApiConfig.cs in App_Start folder on the root of project and add {action} to routeTemplate parameter in MapHttpRoute Method like below :
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);