webapi controller calling wrong method - c#

I have implemented a generic controller using c# and webApi 2.0
http://localhost:4200/api/projects
will correctly call GetAllItems() and return the expected results
http://localhost:4200/api/projects/1
does not call GetItemById( )
Instead, it calls GetAllItems( )
prior to building the generic, i built a concrete controller for projects.
Its a cut/paste and it calls the correct methods.
My thinking is that my route is wrong on the generic, or should be different because it is a generic, but I can not seem to come up w/ the correct syntax.
Why is the generic not calling the correct method when the url includes a trailing integer?
Things i tried w.o success
Reordering the methods
Enhancing the verb to be System.Web.Http.HttpGet
Combining GET and ROUTE into 1 tag, comma separated
Specifying [FromUri] on the itemId parameter in the function signature
Commenting out the GetAllItems() --> The requested resource does not support http method 'GET' (This has to be the big clue, but for the life of me...)
Here is an abbreviated listing of the generic template
[RoutePrefix("api/{contoller}")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class baseController<T, T_Q > : ApiController
where T:pgBaseClass, new()
where T_Q : sbQuery<T> , new()
{
[HttpGet]
[Route("")]
public CustomJsonStringResult GetAllItems()
{
T_Q q = new T_Q();
List<T> l = q.Items();
string json = q.ListToJSON(l);
return JSONStringResultExtension.JSONString(this, json, HttpStatusCode.OK);
}
[HttpGet]
[Route("{itemId:int}")]
public IHttpActionResult GetItemById(int itemId)
{
T_Q q = new T_Q();
T p = q.GetById(itemId);
if (p == null)
{
return JSONStringResultExtension.JSONString(this, "Item not Found", HttpStatusCode.NotFound);
}
else
{
return JSONStringResultExtension.JSONString(this, p.JSON, HttpStatusCode.OK);
}
}
}
Here is the definition for the projects controller using the generic
public class ProjectsController : baseController<pgProject,pgProjectQuery>
{
}
Here is an abbreviated listing of the non generic controller that works as expected.
(I am excluding one or the other to get the project to compile and run...)
[RoutePrefix("api/projects")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ProjectController : ApiController
{
//[HttpGet]
[Route("")]
public CustomJsonStringResult GetAllItems()
{
pgProjectQuery ag = new pgProjectQuery();
ag.SortExpression = " [Name] asc ";
List<pgProject> l = ag.Items();
string json = ag.ListToJSON(l);
return JSONStringResultExtension.JSONString(this, json, HttpStatusCode.OK);
}
[HttpGet]
[Route("{itemId:int}")]
public IHttpActionResult GetItemById(int itemId)
{
pgProjectQuery q = new pgProjectQuery();
pgProject p = q.GetById(itemId);
if (p == null)
{
return JSONStringResultExtension.JSONString(this, "Item not Found", HttpStatusCode.NotFound);
}
else
{
return JSONStringResultExtension.JSONString(this, p.JSON, HttpStatusCode.OK);
}
}
}

I had this configured in webApiConfig.js
here i have the sole route defined w/ an optional parameter called id.
I changed the parameter name in my GetItemById() method and this works.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
[System.Web.Http.HttpGet]
[Route("{id:int}")]
public CustomJsonStringResult GetItemById([FromUri] int id){...}
What I can not explain is why the non generic controller worked as expected but the generic-based controller does not.
I do believe things are probably 'more correct' now, just wish I had a factual answer.
I also ran into the same problem with my patch method. Renamed the parameter to id and it worked.
Hope this helps someone down the line.

Related

Can Derivatives of a Generic Controller define routes

I have defined a generic controller and have a couple derived classes that are working.
This is fabulous.
I now run into the case where I want to add a route to one of the concrete derivations.
It does not seem to want to pick up the new routes. Error messages tell me that it is looking at the right class.
I have defined the generic and a derived class here.
webapi controller calling wrong method
I am about to throw in the towel on this exercise.
Is is possible for the derived classes to add routes? Does anyone know of examples (i know, i cant ask for examples...)
Thanks
paratial listing of the generic
GetItemById() works if I uncomment it
[RoutePrefix("api/{contoller}")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class baseController<T, T_Q, M> : ApiController
where T : pgBaseClass, new()
where T_Q : sbQuery<T>, new()
where M : new()
{
//[System.Web.Http.HttpGet]
//[Route("{id}")]
//public CustomJsonStringResult GetItemById([FromUri] string id)
//{
// T_Q q = new T_Q();
// T p = q.GetById(Convert.ToInt32(id));
// if (p == null)
// {
// return JSONStringResultExtension.JSONString(this, "Item not Found", HttpStatusCode.NotFound);
// }
// else
// {
// return JSONStringResultExtension.JSONString(this, p.JSON, HttpStatusCode.OK);
// }
//}
[System.Web.Http.HttpGet]
[Route("")]
public CustomJsonStringResult GetAllItems()
{
T_Q q = new T_Q();
List<T> l = q.Items();
string json = q.ListToJSON(l);
return JSONStringResultExtension.JSONString(this, json, HttpStatusCode.OK);
}
}
partial listing of one concrete class
public class ProjectsController : baseController<pgProject,pgProjectQuery, Models.mpgProject>
{
[System.Web.Http.HttpGet]
[Route("{id}/cfs")]
public CustomJsonStringResult GetCashFlows([FromUri] string id)
{
pgCashFlowQuery q = new pgCashFlowQuery();
pgCashFlow p = q.GetById( Convert.ToInt32(id ));
if (p == null)
{
return JSONStringResultExtension.JSONString(this, "Item not Found", HttpStatusCode.NotFound);
}
else
{
List<pgCashFlowString> l = p.CashFlowStrings;
pgCashFlowStringQuery q2 = new pgCashFlowStringQuery();
string json = q2.ListToJSON(l);
return JSONStringResultExtension.JSONString(this, p.JSON, HttpStatusCode.OK);
}
}
}
full listing of webApiCOnfig.cs w/ 1 route hard coded
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Collection",
routeTemplate: "api/{controller}/{id}/cfs",
defaults: new { id = RouteParameter.Optional }
);
}
}
When I call the api w/
http://localhost:4200/api/projects/3
I will get the json i expect (except i have it commented out here..)
same for http://localhost:4200/api/projects
I get the list.
now when I call http://localhost:4200/api/projects/3/cfs
the GetAllItems() method is triggered.
This is exactly the example posted by MS at
https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing
where they have
[Route("{id:int}/details")]
[ResponseType(typeof(BookDetailDto))]
public async Task<IHttpActionResult> GetBookDetail(int id)
describing the method that will return details for a book.
Things I have tried
Changing {id:int} to {id} and changing the method parameter to string. This seems to be more of a complicating factor than i want it to be. I am likely conflating this with something else I have yet to understand.
commenting out the hardcoded route. WHen I do this , and during other experiments I get "ANCM In-Process Handler Load Failure". The peculiar thing here is that all articles i read about this talk about .NetCore. This projechappens to be .Net Framework 4.7.2..
The WEB tab on PROPERTIES shows I am launching IIS Express using default bitness.
I really do not see anything that would bring .NetCore into this equation

Web API 2 - Error: "No type was found that matches the controller named 'resize'."

I am trying to just do a simple file upload API using Web API.
Here is the Controller:
[RoutePrefix("api/resize")]
public class ResizeController : ApiController
{
[HttpPost, Route("api/resize/preserveAspectRatio")]
public async Task<IHttpActionResult> resizePreserveAspectRatio()
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
int maxWidth = 100;
int maxHeight = 100;
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
//Do whatever you want with filename and its binaray data.
}
return Ok();
}
}
This is my WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
When I POST a file with PostMan, here is the error I get:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:26303/api/resize/preserveAspectRatio'.",
"MessageDetail": "No type was found that matches the controller named 'resize'."
}
This is not a dupe - was not able to find another article that addresses this specific combination.
This as you would expect is a routing issue. The comments have already identified that you have conflicts with your route and route prefix attributes resulting in the following route
api/resize/api/resize/preserveAspectRatio
being mapped to your action.
To get the desired route, you can either remove the prefix from the controller itself.
//removed prefix
public class ResizeController : ApiController {
//Matches POST api/resize/preserveAspectRatio
[HttpPost, Route("api/resize/preserveAspectRatio")]
public async Task<IHttpActionResult> resizePreserveAspectRatio() {
//...removed for brevity
}
}
Or Remove it from the route on the method
[RoutePrefix("api/resize")]
public class ResizeController : ApiController {
//Matches POST api/resize/preserveAspectRatio
[HttpPost, Route("preserveAspectRatio")]
public async Task<IHttpActionResult> resizePreserveAspectRatio() {
//...removed for brevity
}
}
Or override the route prefix by using tilde (~) on the method attribute
[RoutePrefix("api/resize")]
public class ResizeController : ApiController {
//Matches POST api/resize/preserveAspectRatio
[HttpPost, Route("~/api/resize/preserveAspectRatio")]
public async Task<IHttpActionResult> resizePreserveAspectRatio() {
//...removed for brevity
}
}
Reference Attribute Routing in ASP.NET Web API 2

Why am I getting an error when calling a webservice function?

I am coding a C# web api 2 webservice and would like some help to get a single item from a request to a webservice.
Here is the web service controller class code:
[RoutePrefix("api")]
public class ItemsWebApiController : ApiController
Here is the web service function:
// GET: api/Getitem/1
[Route("Getitem")]
[System.Web.Http.HttpGet]
[ResponseType(typeof(Item))]
public async Task<IHttpActionResult> GetItem(int id)
{
Item item = await db.items.FindAsync(id);
if (item == null)
{
return NotFound();
}
return Ok(item);
}
Here is the uri to the IIS website:
http://localhost/thephase
Here is the uri that I am accessing:
http://localhost/thephase/api/Getitem/1
Here is the error that is being displayed in the browser:
{"Message":"No HTTP resource was found that matches the request URI
'http://localhost/thephase/api/GetItem/1'.","MessageDetail":"No type
was found that matches the controller named 'GetItem'."}
Here is the WebApiConfig code:
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 }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
The error states that the controller is named 'GetItem', and this is incorrect. Hence I am thinking that the problem is in the WebApiConfig route code.
If I remove the int id from the function, then the function is called correctly.
Here is the same function with no parameter:
// GET: api/Getitemnoparameter
[Route("Getitemnoparameter")]
[System.Web.Http.HttpGet]
[ResponseType(typeof(Item))]
public async Task<IHttpActionResult> GetItem()
{
Item item = await db.items.FindAsync(1);
if (item == null)
{
return NotFound();
}
return Ok(item);
}
The following uri accesses the function correctly:
http://localhost/thephase/api/Getitemnoparameter
So the problem has something to do with the int parameter.
Can someone please help me to get access the GetItem function with a parameter?
Because you are using Attribute Routing you also need to specify the parameter for it to work.
Check this tutorial out for a better understanding.
Route Prefixes
[Route("Getitem/{id:int}")]
The Id is an int. int is a value type. Value types cannot be null. You set the parameter to RouteParameter.Optional, but if it's optional, it must be able to be assigned null.
Solution: Use a nullable int
public async Task<IHttpActionResult> GetItem(int? id)

No action was found on the controller 'Depts' that matches the request. webApi C# .NET

I have web api with three methods it was running absolutely very fine a week before but I don't understand why it is not running now. I am trying from last 3 days and desperately looking for help.
when testing on browser for
/api/Depts/Get/
/api/Depts/GetDeptById/2
worked fine but tested for
api/Depts/GetDeptByCompany/'abc'
Throwing an error No action was found on the controller 'Attorneys' that matches the request. webApi C# .NET
weapi.Config file
namespace Test_Dept
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//// Attribute routing
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
// routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Controller
namespace Test_Dept.Controllers
{
public class DeptsController : ApiController
{
[HttpGet]
public List<Dept> Get()
{
return DeptRepository.GetAllDepts();
}
[HttpGet]
public Dept GetDeptById(int id)
{
Dept Att = DeptRepository.GetDeptById(id);
if(Att==null)
throw new HttpResponseException(HttpStatusCode.BadRequest);
else
return Att;
}
[HttpGet]
public List<Dept> GetDeptByCompany(string company)
{
var Att = DeptRepository.GetDeptByCompany(company);
if (Att.ToList().Count == 0)
throw new HttpResponseException(HttpStatusCode.BadRequest);
else
return Att;
}
Repository
namespace Test_Dept.Models
{
public class DeptRepository
{
public static List<Dept> GetAllDepts()
{
DeptEntities dataContext = new DeptEntities();
var query = from dept in dataContext.Depts select dept;
return query.ToList();
}
public static Dept GetDeptById(int id)
{
DeptEntities dataContext = new DeptEntities();
var query = (from dept in dataContext.Depts where Dept.ID == id select dept).SingleOrDefault();
return query;
}
public static List<Dept> GetDeptByCompany(string company)
{
DeptEntities dataContext = new DeptEntities();
var query = (from dept in dataContext.Depts where Dept.Company == company select Dept);
return query.ToList();
}
Assuming that the mention of "attorneys" in your question is a typo. The parameter name in your route needs to match the parameter name in your controller action. You existing route will only work for controller actions where the parameter name is id. If you want the url api/Depts/GetDeptByCompany/abc to work (notice there are no quotes around the parameter). Then you could add a route such as
config.Routes.MapHttpRoute(
name: "CompanyApi",
routeTemplate: "api/{controller}/{action}/{company}");
If you are using Web API 2 then yoou may prefer attribute routes and you can do the following on the controller action.
[HttpGet]
[Route("api/{controller}/{action}/{company}")]
public List<Dept> GetDeptByCompany(string company)
need to remove quotes and test the web api method like
api/Depts/GetDeptByCompany?company=abc and it worked. Thank you every one for helping me to find the answer you thoughts and comments helped me to solve this.

Multiple HttpPost method in Web API controller

I am starting to use MVC4 Web API project, I have controller with multiple HttpPost methods. The Controller looks like the following:
Controller
public class VTRoutingController : ApiController
{
[HttpPost]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
Here MyRequestTemplate represents the template class responsible for handling the Json coming through the request.
Error:
When I make a request using Fiddler for http://localhost:52370/api/VTRouting/TSPRoute or http://localhost:52370/api/VTRouting/Route I get an error:
Multiple actions were found that match the request
If I remove one of the above method it works fine.
Global.asax
I have tried modifying the default routing table in global.asax, but I am still getting the error, I think I have problem in defining routes in global.asax. Here is what I am doing in global.asax.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "MyTSPRoute",
routeTemplate: "api/VTRouting/TSPRoute",
defaults: new { }
);
routes.MapHttpRoute(
name: "MyRoute",
routeTemplate: "api/VTRouting/Route",
defaults: new { action="Route" }
);
}
I am making the request in Fiddler using POST, passing json in RequestBody for MyRequestTemplate.
You can have multiple actions in a single controller.
For that you have to do the following two things.
First decorate actions with ActionName attribute like
[ActionName("route")]
public class VTRoutingController : ApiController
{
[ActionName("route")]
public MyResult PostRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[ActionName("tspRoute")]
public MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
Second define the following routes in WebApiConfig file.
// Controller Only
// To handle routes like `/api/VTRouting`
config.Routes.MapHttpRoute(
name: "ControllerOnly",
routeTemplate: "api/{controller}"
);
// Controller with ID
// To handle routes like `/api/VTRouting/1`
config.Routes.MapHttpRoute(
name: "ControllerAndId",
routeTemplate: "api/{controller}/{id}",
defaults: null,
constraints: new { id = #"^\d+$" } // Only integers
);
// Controllers with Actions
// To handle routes like `/api/VTRouting/route`
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}"
);
Another solution to your problem would be to use Route which lets you specify the route on the method by annotation:
[RoutePrefix("api/VTRouting")]
public class VTRoutingController : ApiController
{
[HttpPost]
[Route("Route")]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost]
[Route("TSPRoute")]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
use:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
it's not a RESTful approach anymore, but you can now call your actions by name (rather than let the Web API automatically determine one for you based on the verb) like this:
[POST] /api/VTRouting/TSPRoute
[POST] /api/VTRouting/Route
Contrary to popular belief, there is nothing wrong with this approach, and it's not abusing Web API. You can still leverage on all the awesome features of Web API (delegating handlers, content negotiation, mediatypeformatters and so on) - you just ditch the RESTful approach.
A web api endpoint (controller) is a single resource that accepts get/post/put/delete verbs. It is not a normal MVC controller.
Necessarily, at /api/VTRouting there can only be one HttpPost method that accepts the parameters you are sending. The function name does not matter, as long as you are decorating with the [http] stuff. I've never tried, though.
Edit: This does not work. In resolving, it seems to go by the number of parameters, not trying to model-bind to the type.
You can overload the functions to accept different parameters. I am pretty sure you would be OK if you declared it the way you do, but used different (incompatible) parameters to the methods. If the params are the same, you are out of luck as model binding won't know which one you meant.
[HttpPost]
public MyResult Route(MyRequestTemplate routingRequestTemplate) {...}
[HttpPost]
public MyResult TSPRoute(MyOtherTemplate routingRequestTemplate) {...}
This part works
The default template they give when you create a new one makes this pretty explicit, and I would say you should stick with this convention:
public class ValuesController : ApiController
{
// GET is overloaded here. one method takes a param, the other not.
// GET api/values
public IEnumerable<string> Get() { .. return new string[] ... }
// GET api/values/5
public string Get(int id) { return "hi there"; }
// POST api/values (OVERLOADED)
public void Post(string value) { ... }
public void Post(string value, string anotherValue) { ... }
// PUT api/values/5
public void Put(int id, string value) {}
// DELETE api/values/5
public void Delete(int id) {}
}
If you want to make one class that does many things, for ajax use, there is no big reason to not use a standard controller/action pattern. The only real difference is your method signatures aren't as pretty, and you have to wrap things in Json( returnValue) before you return them.
Edit:
Overloading works just fine when using the standard template (edited to include) when using simple types. I've gone and tested the other way too, with 2 custom objects with different signatures. Never could get it to work.
Binding with complex objects doesn't look "deep", so thats a no-go
You could get around this by passing an extra param, on the query string
A better writeup than I can give on available options
This worked for me in this case, see where it gets you. Exception for testing only.
public class NerdyController : ApiController
{
public void Post(string type, Obj o) {
throw new Exception("Type=" + type + ", o.Name=" + o.Name );
}
}
public class Obj {
public string Name { get; set; }
public string Age { get; set; }
}
And called like this form the console:
$.post("/api/Nerdy?type=white", { 'Name':'Slim', 'Age':'21' } )
It is Possible to add Multiple Get and Post methods in the same Web API Controller. Here default Route is Causing the Issue. Web API checks for Matching Route from Top to Bottom and Hence Your Default Route Matches for all Requests. As per default route only one Get and Post Method is possible in one controller. Either place the following code on top or Comment Out/Delete Default Route
config.Routes.MapHttpRoute("API Default",
"api/{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
When creating another Http Method add [HttpPost("Description")]
[HttpPost("Method1")]
public DataType Method1(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost("Method2")]
public DataType Method2(MyRequestTemplate routingRequestTemplate){}
Put Route Prefix [RoutePrefix("api/Profiles")] at the controller level and put a route at action method [Route("LikeProfile")]. Don't need to change anything in global.asax file
namespace KhandalVipra.Controllers
{
[RoutePrefix("api/Profiles")]
public class ProfilesController : ApiController
{
// POST: api/Profiles/LikeProfile
[Authorize]
[HttpPost]
[Route("LikeProfile")]
[ResponseType(typeof(List<Like>))]
public async Task<IHttpActionResult> LikeProfile()
{
}
}
}
You can use this approach :
public class VTRoutingController : ApiController
{
[HttpPost("Route")]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost("TSPRoute")]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
I think the question has already been answered. I was also looking for something a webApi controller that has same signatured mehtods but different names. I was trying to implement the Calculator as WebApi. Calculator has 4 methods with the same signature but different names.
public class CalculatorController : ApiController
{
[HttpGet]
[ActionName("Add")]
public string Add(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Add = {0}", num1 + num2);
}
[HttpGet]
[ActionName("Sub")]
public string Sub(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Subtract result = {0}", num1 - num2);
}
[HttpGet]
[ActionName("Mul")]
public string Mul(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Multiplication result = {0}", num1 * num2);
}
[HttpGet]
[ActionName("Div")]
public string Div(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Division result = {0}", num1 / num2);
}
}
and in the WebApiConfig file you already have
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
Just set the authentication / authorisation on IIS and you are done!
Hope this helps!
Best and simplest explanation I have seen on this topic -
http://www.binaryintellect.net/articles/9db02aa1-c193-421e-94d0-926e440ed297.aspx
Edited -
I got it working with only Route, and did not need RoutePrefix.
For example, in the controller
[HttpPost]
[Route("[action]")]
public IActionResult PostCustomer
([FromBody]CustomerOrder obj)
{
}
and
[HttpPost]
[Route("[action]")]
public IActionResult PostCustomerAndOrder
([FromBody]CustomerOrder obj)
{
}
Then, the function name goes in jquery as either -
options.url = "/api/customer/PostCustomer";
or
options.url = "/api/customer/PostCustomerAndOrder";
I am using .Net6. please find the following code. I have achieve like the following.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ProjectName.Controllers
{
[Route("[controller]")]
[ApiController]
public class WizardAPIController : ControllerBase
{
[HttpGet("Methord1")]
public async Task<IActionResult> Methord1()
{
return Ok("all good");
}
[HttpGet("Methord2")]
public async Task<IActionResult> Methord2()
{
return Ok("all good");
}
}
}
public class Journal : ApiController
{
public MyResult Get(journal id)
{
return null;
}
}
public class Journal : ApiController
{
public MyResult Get(journal id, publication id)
{
return null;
}
}
I am not sure whether overloading get/post method violates the concept of restfull api,but it workds. If anyone could've enlighten on this matter. What if I have a uri as
uri:/api/journal/journalid
uri:/api/journal/journalid/publicationid
so as you might seen my journal sort of aggregateroot, though i can define another controller for publication solely and pass id number of publication in my url however this gives much more sense. since my publication would not exist without journal itself.

Categories