Only first POST method of controller is called - c#

I have a couple of post methods in my controller. Whenever I post data to this controller, only the first POST method is called. My requirement is to call the second method as the parameters are going to be different for both the methods. Here is the route config:
config.Routes.MapHttpRoute(
name: "AddUser",
routeTemplate: "api/users/adduser",
defaults: new { controller = "users" }
);
config.Routes.MapHttpRoute(
name: "ChangeUser",
routeTemplate: "api/users/changeuser",
defaults: new { controller = "users" }
);
This is my controller's code:
[AllowAnonymous]
[ActionName("adduser")]
public string PostDetails(JObject userData)//Always this method is called.
{
//My code here
}
[AllowAnonymous]
[ActionName("changeuser")]
public string ChangeUser(int userId)
{
//My code here
}
This is called from the view:
Ext.Ajax.request( { url: 'localhost/myapp/api/users/changeuser'
, mode: 'POST'
, params: { userID: 1 }
}
);

Adding constrains in the route config will solve your problem. Try below config..
config.Routes.MapHttpRoute(
name: "AddUser",
routeTemplate: "api/{controller}/{action}",
defaults: new { },
constraints: new { controller = "users", action = "adduser" }
);
config.Routes.MapHttpRoute(
name: "ChangeUser",
routeTemplate: "api/{controller}/{action}",
defaults: new { },
constraints: new { controller = "users", action = "changeuser" }
);
The C# part:
[AllowAnonymous]
[ActionName("adduser")]
[AcceptVerbs("Post")]
public string PostDetails(JObject userData)//Always this method is called.
{
//My code here
}
[AllowAnonymous]
[ActionName("changeuser")]
[AcceptVerbs("Post")]
public string ChangeUser(int userId)
{
//My code here
}

Try:
[HttpPost, ActionName("Name")]
instead of:
[ActionName("Name")]
I'm not an expert but it might work this way.

Related

Two GET functions in C# MVC

public IEnumerable<Employee> GetEmployees()
{
//code and return
}
public Employee GetSingleEmployee(int Id)
{
//code and return
}
This is what I have now. I'm trying to make the app call the first function with a get call api/employee and the second one with a get call api/employee/(an ID number) eg api/employee/75. The get call always goes to the first one. How do I solve this?
This is my routing:
namespace EmployeeApp
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id=UrlParameter.Optional }
);
}
}
}
Use annotation like
[HttpGet]
[Route("employee")]
public IEnumerable<Employee> GetEmployees()
{
//code and return
}
[HttpGet]
[Route("employee/{Id}")]
public Employee GetSingleEmployee(int Id)
{
//code and return
}
On class above you RoutePrefix to
[RoutePrefix("api")]
As others have stated without seeing your routing it could be difficult to guess but assuming it is something like this:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
the optional id parameter should work as long as the method parameter is lowercase.
If you want to use the action as part of the routing then use this, placing it above the default route:
routes.MapHttpRoute(
name: "Api By Action",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

How define a WebApi route to access method

I'd like have access to a method other than "GET", "PUSH", "PATCH", ....
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Employee",
routeTemplate: "api/employee/{employeeid}",
defaults: new { controller = "employee", employeeid = RouteParameter.Optional }
);
//for test : not work
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);
//JSON Formatting
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
I have access to the employee controller :
[RoutePrefix("api/employee")]
public class EmployeeController : ApiController
{
public HttpResponseMessage Get() { }
public HttpResponseMessage Get(int employeeid) {}
public HttpResponseMessage Post([FromBody] EmployeeModel model){}
[HttpPut]
[HttpPatch]
public HttpResponseMessage Patch([FromBody] EmployeeModel model){}
[Route("initialisation")]
public HttpResponseMessage Initialisation() {}
}
I have access without any problem :
http://localhost/employee
http://localhost/employee/1
I'd like have access to the "Initialisation" method :
http://localhost/employee/initialisation
I added the route "DefaultApi" but when I try I get this error :
{
"$id": "1",
"message": "The request is invalid.",
"messageDetail": "The parameters dictionary contains a null entry for parameter 'employeeid' of non-nullable type 'System.Int32'
for method 'System.Net.Http.HttpResponseMessage Get(Int32)' in 'Pme.WebApi.Controllers.EmployeeController'.
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
Thanks,
Try the following:
Change
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);
To
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{employeeid}",
defaults: new { action = "get", employeeid= RouteParameter.Optional }
);
The only change the name of last ID parameter from "id" to "employeeid"
I think this should work out. You can try modifying your WebApiConfig like this :
config.Routes.MapHttpRoute(
name: "DefaultApi",
//routeTemplate: "api/{controller}/{id}",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
This way, you keep it open for both, the controller name and the action name to be something dynamic and try hitting the URL that would be generated as per this format.
UPDATE :
Comment the "Employee" route code. Just keep the "DefaultApi" route active. Now try to hit this URL :
http://localhost:1955/api/Employee/Create/parameters
You obviously need to keep the host name as per yours and the parameter too.
Hope this helps.
You need to register attribute routing in your WebApiConfig
config.MapHttpAttributeRoutes();
See Attribute Routing in ASP.NET Web API 2
Alternatively you can specify exactly where your route should go
config.Routes.MapHttpRoute("Initialisation", "api/employee/initialisation",
new {controller = "employee", action = "initialisation"});

Nested functions in controller

New to MVC and the API template, familiar with C#.
Plain new WebAPI project with a simple controller. Url path like /api/Clients/1, is there a way to sort of nest controllers? Or execute a function in the same controller by following a url path like this: /api/Clients/1/Sysinfo/typeOf?
I have a model Client which contains properties which are sysinfo items. /api/Clients/1 returns all the properties of an object Client with the Id of 1. I want only specific items returned with /api/Clients/1/Sysinfo/RAM for example.
#Joachim Rosskopf
I have tried that approach. It result in a 404. /clients/1/ works. /clients/1/sysinfo does not work.
Using the following routes:
routes.MapRoute(
name: "Sysinfo",
url: "Clients/{id}/Sysinfo/{type}",
defaults: new { controller = "Sysinfo", type = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Created a new controller SysinfoController:
public class SysinfoController : ApiController
{
public Sysinfo GetAllSysinfoItems()
{
return new Sysinfo { Id = 1, RAM = "1GB" };
}
public IHttpActionResult GetSysinfoByType(int id)
{
return Ok();
}
}
You have to adjust your routing configuration and add special entries for the child routes. It is important to add the most specific route first:
routes.MapHttpRoute(
name: "SysInfoApi",
routeTemplate: "api/Clients/{id}/Sysinfo/{param}",
defaults: new { controller = "SysInfo", param = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In the Above example there is a specific route for a SysInfo controller, which handles the nested resource. As well as the default route.
The SysInfo controller with actions for GET-requests could look the following
public class SysInfoController : ApiController
{
// GET api/client/{id}/sysinfo/
public string Get(int id)
{
return "value";
}
// GET api/client/{id}/sysinfo/{param}
public string Get(int id, string param)
{
return "value";
}
}
I don't know of a method to handle hierarchical ressources in WebApi automatically.

Is there a way to tell routing that my default action name is equal to the HTTP Action Verb?

I've looked through this document on MSDN and can't come up with the answer.
Considering that I have a route defined like this:
config.Routes.MapHttpRoute(
name: "DefaultWithActionAndID",
routeTemplate: "v{version}/{controller}/{action}/{id}",
defaults: null,
constraints: new {
action = #"[a-zA-Z]+",
id = #"\d+"
}
);
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "v{version}/{controller}/{id}",
defaults: null,
constraints: new {
id = #"\d+"
}
);
config.Routes.MapHttpRoute(
name: "DefaultWithoutActionOrId",
routeTemplate: "v{version}/{controller}",
defaults: null,
);
Now I have two controllers that looks like this:
public class ItemController:ApiController{
[HttpGet]
public Item Get(int id){}
[HttpGet]
public Item GetSomething(int id){}
[HttpPut]
public Item Put(Item newItem){}
}
public class AnotherController:ApiController{
[HttpPut]
public HttpResponseMessage Put(Something item){}
}
I'd like to be able to call all of these endpoints like this:
GET /api/Item/344
GET /api/Item?id=344
GET /api/Item/Something/2334
GET /api/Item/Something?id=2334
PUT /api/Item body={newItem}
PUT /api/Another body={newSomething}
This will work, but only if I add "Get" as the default action name. If I do not specify a default action name in my route, then it complains about multiple matching action names. If I do add the default action name, then I cannot call PUT to the Put() method without an error because the action name doesn't match the default and isn't found.
// Will work in some cases, but not all
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "v{version}/{controller}/{id}",
defaults: new {
action="Get",
id=RouteParameters.Optional
},
constraints: new {
id = #"\d+"
}
);
// Works
GET /api/Item/344
GET /api/Item?id=344
GET /api/Item/Something/2334
GET /api/Item/Something?id=2334
// Doesn't work
PUT /api/Item body={newItem}
PUT /api/Another body={newSomething}
How can I tell Routing to use the Action with the name that matches my HTTP Verb, if one exists before trying to use
If you define your routes as follows:
config.Routes.MapHttpRoute(
name: "DefaultWithActionAndID",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new {action = #"[a-zA-Z]+", id = #"\d*" }
);
config.Routes.MapHttpRoute(
name: "DefaultWithID",
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "GET", id = RouteParameter.Optional },
constraints: new { id = #"\d*", httpMethod = new HttpMethodConstraint(new string[] { "GET" }) }
);
config.Routes.MapHttpRoute(
name: "DefaultWithoutActionOrId",
routeTemplate: "api/{controller}",
defaults: new { action = "PUT" },
constraints: new { httpMethod = new HttpMethodConstraint(new string[] { "PUT" }) }
);
And also place the ActionName attribute on your GetSomething method as so:
[ActionName("Something")]
public Item GetSomething(int id){}
You should then be able to hit all the endpoints mentioned above.
The way I see it you'd need the following setup:
1.
/api/Item/344
{controller}/{id}
2.
/api/Item/Something/2334
{controller}/{action}/{id}
and decorate the 'GetSomething' method as follows:
[ActionName("Something")]
public Item GetSomething(int id){}
3.
/api/Item?id=344
/api/Item/Something?id=2334
I'm not entirely sure about these - have you tried adding a default to the routes above:
defaults: new { id = RouteParameter.Optional }
4.
I'd expect PUT to just work if #3 is applied
Let me know if that changes anything.

MVC Web Api Route with default action not working

In my TestController I have the following:
[HttpGet]
public IEnumerable<String> Active()
{
var result = new List<string> { "active1", "active2" };
return result;
}
[HttpGet]
public String Active(int id)
{
var result = new List<string> { "active1", "active2" };
return result[id];
}
In RouteConfig the mapping is:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "", id = RouteParameter.Optional });
In a browser the following request works:
api/test/active/1
But this returns a Internal Server Error:
api/test/active
What do you have to do to return a action that may or maynot have a parameter in a similar manner to the default Get?
Update 1
As Cuong Le suggested, changing the ordering of routes helped, the routes are now:
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApi",
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 }
);
I had to remove action = "" from the ActionApi route otherwise the standard Get on the other controllers stopped working (i.e. api/values)
api/test/active is now resolving, but I now get a 500 Internal Server Error for /api/test is it possile to have both resolves, so api/test would return "all" and /test/active only return "some"?
It is probably getting confused since you have two methods named action. Try deleting or renaming one of them and see if that works.
One way to do it is to provide a default value for the parameter,
[HttpGet]
public String Active(int id = 0)
{
var result = new List<string> { "active1", "active2" };
if (id == 0) {
return result;
} else {
return result[id];
}
}

Categories

Resources