This question could have been answered hundred times, but I couldnt find a proper resource. In a WebApi project (default project provided by VS) I have the ValuesController as below.
public string Get(int id)
{
return "value";
}
[HttpGet]
public string FindByName(string name)
{
return name;
}
[HttpGet]
public string FindById(int id)
{
return id.ToString();
}
In the WebApiConfig.cs, I have following route mapping.
config.Routes.MapHttpRoute(
name: "actionApiById",
routeTemplate: "api/{controller}/{action}/{Id}",
defaults: new { action = "FindById", Id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "actionApi",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: new { name = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Now only the FindById() action is working when i try in the browser. Why does the rest of api calls return "No HTTP resource was found that matches the request"
How can I get all three methods working? without using AttributeRouting. Am I lack of basic concepts of web api? ( i think yes)
AS We all know REST is resource based and this identify the resource with the URL, so that not more than one method with same parameter will be allowed in the REST service but there is work around in MVC 5 Web Api method level routing.
Here is the example you can do that:
[HttpGet]
[Route("api/search/FindByName/{name}")]
FindByName(string name)
{
}
[HttpGet]
[Route("api/search/FindById/{name}")]
FindById(int searchId)
Note:"search" is the controller name.
Please let know if need more clarification.
In general you don't want to have a route per action like your sample suggests. As your app grows this will get quickly out of hand.
Also consider building your url space in a way that will look just RESTfull
So methods will be GetById, GetByName, and then pass the parameters in the query string to match the right action (BTW not sure what the difference in your case is between GetById and FindById if they are not really different consider just keeping one of them around).
You can stick with the default route and your request will look like:
/api/controller/345 or /api/controller?name=UserName or /api/controller?SearchId=345 (assuming search was indeed a different behavior)
Then the method signatures:
Get(int id)
{
}
[HttpGet]
FindByName(string name)
{
}
[HttpGet]
FindById(int searchId)
{
}
Your actionApiById Route also matches the actionApi route, As your id is integer try using constraint like this.
config.Routes.MapHttpRoute(
name: "actionApiById",
routeTemplate: "api/{controller}/{action}/{Id}",
defaults: new { action = "FindById", Id = RouteParameter.Optional }
constraints: new {Id = #"\d+" }
);
Related
I want to make a web api that is passed 4 parameters.
Here is my route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{email}/{firstname}/{lastname}/{source}"
);
Here is the method signature
public string GetId(string email, string firstname, string lastname, string source)
Here is the calling url
http://fakedomain.com/api/Contacts/GetId?email=user#domain.com&firstname=joe&lastname=shmoe&source=123
I get a 404 error.
If I set each parameter to optional in the route config, and set up each argument with a default value it gets called. However, each argument gets the default value and not the passed value.
I feel like I am close, what am I missing?
You don't need a special routing record to handle multiple parameters. The routing record you created would be looking for the following route
/api/controller/Dan#dan.com/Dan/FunnyLastName/TheCoffeeShop
but you are trying to pass in parameters, not specify a route.
with this routing record:
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional, action = "DefaultAction" });
the following GET endpoint:
public HttpResponseMessage Get(int requestId = 0, string userName = null, string departmentName = null, bool includeCompleted = false)
{
//code
}
could be hit as :
/api/controllername/?requestId=15&username=Dan
or
/api/controllername/?departmentName=SoftwareEngineering
or any other combination of the parameters (or no parameters since they have default values)
Since you have a "Named" action (GetId) instead of the default actions (GET,POST,PUT..), this complicates things a little bit and you would have to work out a custom route to handle the action name. The following is what I use for custom action names (id is required in this example)
config.Routes.MapHttpRoute("ActionRoute", "api/{controller}/{action}/{id}");
Your endpoint would have to explicitly accept one parameter with the name 'id'
public HttpResponseMessage LockRequest(int id, bool markCompleted)
{
//code
}
This endpoint would be hit at the following route:
/api/controllerName/LockRequest/id?markCompleted=true
Following the RESTful spec, it is better to stay away from custom action names when possible. Most of the time you can get away with the normal HTTP verbs and just use named actions to manipulate existing items (hence why ID is required in my example). For your code you could just have two GET endpoints, one that takes a specific ID to get the item, one that returns all items (including ids) based on "search parameters".
public HttpResponseMessage Get(int id)
public HttpResponseMessage Get(int requestId = 0, string userName = null, string departmentName = null, bool includeCompleted = false)
These would both be handled by the default routing record.
Ensure you have default api route setting in WebApiConfig.cs file.
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 }
);
config.Routes.MapHttpRoute(
name: "ContactApi",
routeTemplate: "api/{controller}/{email}/{firstname}/{lastname}/{source}"
);
}
}
http://fakedomain.com/api/Contacts/GetId?email=user#domain.com&firstname=joe&lastname=shmoe&source=123
Note : I replaced fakedomain with localhost and it works... (localhost/api/Contacts/GetId?email=user#domain.com&firstname=joe&lastname=shmoe&source=123)
public class Parameters
{
public int Param1 { get; set; }
public string Param2 { get; set; }
}
and then in your controller method:
[ActionName("DoSomething")]
[HttpPost]
public IHttpActionResult DoSomething(Parameters myParameters)
{
var x = myParameters.Param1;
var y = myParameters.Param1;
//do something else..
}
And build a ajax call like this:
var request = {
Param1 : "1",
Param2 : "Mystring"
};
function callToMethodController(request) {
var deferred = $q.defer();
$http.post('api/object/DoSomething', request)
.success(function (data) {
deferred.resolve(data);
}).error(function (error) {
deferred.reject('There was an error.');
});
return deferred.promise;
}
I am new in web api and I am creating a demo with default Web API, I see that ValuesController has default 4 functions Get,Post,Put and Delete. I see that the ValuesController impleament 4 function to ApiController which can not modify. So, Can I write some more functions like search item by price or model ? If can, what url on browser to run debug for new function ?
thankyou
It sounds like you need to add new actions to your controller. You will need to modify (or add to) your Routes in your WebApiConfig.cs file to include action mapping.
For example, let's say you update your Controller with the additional functions GetTestString1 and GetTestString2:
public class TestController : ApiController
{
public String GetTestString1(int id)
{
return "Test String 1 for " + id;
}
public String GetTestString2(int id)
{
return "Test String 2 for " + id;
}
}
To perform the routing to these new functions you need to add the following to the WebApiConfig.cs file in the App_Start folder:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// This mapping entry may already exist for you. Leave it alone
// so your existing default functions continue to work properly.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Add this to route your new functions:
config.Routes.MapHttpRoute(
name: "TestStringApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
The above routeTemplate "api/{controller}/{action}/{id}" is how the request URL will be routed to your controller and actions (functions). The {controller} part of the URL will route to your TestController class. The {action} part of the URL is the additional function you asked about and will be mapped to GetTestString1 or GetTestString2 depending on what you request in your URL.
When you open your browser to this address,
http://localhost:60303/api/Test/GetTestString1/100
the route you registered in the WebApiConfig.cs will map the url to your Testcontroller's GetTestString1 action (function) with 100 as the input parameter and will return "Test String 1 for 100" to the browser.
You can call your Testcontroller's GetTestString2 action (function) like this
http://localhost:60303/api/Test/GetTestString2/101
and "Test String 2 for 101" will be returned to the browser.
You can learn more about the how the default functions work (get, post delete) and more about actions and parameters here:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
Here are some related discussions similar to this topic:
Routing with Multiple Parameters using ASP.NET MVC
Routing with action after id parameter in Web API
The 1st route works.
e.g. api/Shelves/SpaceTypes/1
The 2nd route doesn't work. I get multiple actions error.
e.g api/Shelves/1
Q) Why?
These are my routes:
config.Routes.MapHttpRoute(
"DefaultApiWithAction",
"api/{controller}/{action}/{id}"
);
config.Routes.MapHttpRoute(
"DefaultApiWithId",
"api/{controller}/{id}",
null,
new { id = #"\d+" }
);
This is my controller:
public HttpResponseMessage Get(int id)
{
...
}
[ActionName("SpaceTypes")]
public HttpResponseMessage GetSpaceTypes(int id)
{
...
}
For MVC 4.5 this is the only thing that works
There is currently a bug about this.
In order to get your routing to work so the following work
api/Shelves/ //Get All Shelves
api/SpaceTypes/1 //Get Shelf of id 1
api/Shelves/1/SpaceTypes/ //Get all space types for shelf 1
you need to do the following.
Change your routing over to. (Note the default action..)
config.Routes.MapHttpRoute(
name : "DefaultAPi",
routeTemplate : "api/{controller}/{id}/{action}",
defaults: new {id= RouteParameter.Optional,
action = "DefaultAction"}
);
In your controller change the base methods over to
[ActionName("DefaultAction")]
public string Get()
{
}
[ActionName("DefaultAction")]
public string Get(int id)
{
}
[ActionName("SpaceTypes")]
public string GetSpaceTypes(int id)
{
}
Now everything should work as expected..
Thanks to Kip Streithorst full this, for a full explanation
I had a similar issue and discovered i wasn't calling MapHttpAttributeRoutes method in my WebApiConfig...
hope it helps,
David
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
#Kristof is almost right. You should make your second route:
config.Routes.MapHttpRoute(
"DefaultApiWithId",
"api/{controller}/{id}",
new { action = "Get" },
new { id = #"\d+ }
);
This route does not know which action to bind to :
config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = #"\d+" });
Both of your methods are a valid candidate.
I'm not 100% clear what your setup is but in normal REST every resource has a controller, it seems like you have 1 controller with 2 resources.
To make it work in this setup you could force your second route to the get action like this :
config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = #"\d+", action="Get" });
Make sure in your project's Global.asx file, that you've added
WebApiConfig.Register(GlobalConfiguration.Configuration);
into the Application_Start function.
I've tried everything but looks like I'm just not getting it at all. My WebApiConfig.cs looks like this:
config.Routes.MapHttpRoute(
"Default",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute(
"AccountVerification",
"api/{controller}/{id}/{action}",
null,
new { controller = "Account" });
And my controller looks like this:
public class AccountController : ApiController {
public HttpResponseMessage GetByKey(Guid accountID) {
...
}
[HttpGet]
[ActionName("Verify")]
public HttpResponseMessage VerifyAccount(Guid accountID) {
...
}
}
These methods should get a hit with the following URLs:
GET /api/account - WORKS
GET /api/account/00000000-0000-0000-000000000001 - WORKS
GET /api/account/00000000-0000-0000-000000000001/verify - DOESNT WORK
I've tried a lot of things; I am definitely doing something wrong here...please help.
First, if you want to test with fake Guids, as well as having optional Guid parameters, they must be Nullable parameters (fake guids will be deserialized as null) :
public class AccountController : ApiController
{
public HttpResponseMessage GetByKey(Guid? accountID)
{
throw new Exception("GetByKey " + (accountID.HasValue ? accountID.ToString() : "NULL"));
}
[System.Web.Http.HttpGet]
[System.Web.Http.ActionName("Verify")]
public HttpResponseMessage VerifyAccount(Guid? accountID)
{
throw new Exception("VerifyAccount "+(accountID.HasValue?accountID.ToString():"NULL"));
}
}
then, your mapping should :
use the most specific route first
use the correct parameters names
use the correct defaults for actions
config.Routes.MapHttpRoute(
"AccountVerification",
"api/{controller}/{accountID}/{action}"
);
config.Routes.MapHttpRoute(
"Default",
"api/{controller}/{accountID}",
defaults: new { Controller="Account", action = "GetByKey", accountID = RouteParameter.Optional }
);
GET /api/account/00000000-0000-0000-000000000001/verify is matching the first route in your routing collection. Therefore, it never inspects the second route to see if it matches. Make "api/{controller}/{id}/{action}" the first route in your collection and it should work correctly.
In ASP.NET MVC, we have #Url.Action for actions. Is there something similar like #Url.Api which would route to /api/controller?
The ApiController has a property called Url which is of type System.Web.Http.Routing.UrlHelper which allows you to construct urls for api controllers.
Example:
public class ValuesController : ApiController
{
// GET /api/values
public IEnumerable<string> Get()
{
// returns /api/values/123
string url = Url.Route("DefaultApi", new { controller = "values", id = "123" });
return new string[] { "value1", "value2" };
}
// GET /api/values/5
public string Get(int id)
{
return "value";
}
...
}
This UrlHelper doesn't exist neither in your views nor in the standard controllers.
UPDATE:
And in order to do routing outside of an ApiController you could do the following:
public class HomeController : Controller
{
public ActionResult Index()
{
string url = Url.RouteUrl(
"DefaultApi",
new { httproute = "", controller = "values", id = "123" }
);
return View();
}
}
or inside a view:
<script type="text/javascript">
var url = '#Url.RouteUrl("DefaultApi", new { httproute = "", controller = "values", id = "123" })';
$.ajax({
url: url,
type: 'GET',
success: function(result) {
// ...
}
});
</script>
Notice the httproute = "" route token which is important.
Obviously this assumes that your Api route is called DefaultApi in your RegisterRoutes method in Global.asax:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
It works with the simpler form of Url.Action thus you don't have to reference any Routing names:
Url.Action("ActionName", "ControllerName", new { httproute = "DefaultApi" })
You might want to add an area = "" if the URL is needed within an Area. (Api controllers are outside of Areas by default.) I'm using MVC 4.
Want to be able to generate links in a typesafe manner, without hardcoded strings (controller names)?
There's a nuget for that! (and it's written by Mark Seeman)
https://github.com/ploeh/Hyprlinkr
Works like this:
Routes, as usual:
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
Get an URL:
var linker = new RouteLinker(request);
var uri = linker.GetUri<FooController>(r => r.GetById(1337));
Result:
http://localhost/api/foo/1337
Here is the KISS method for answering the question:
If this is the code that you would use to create a MVC controller URL
#Url.Action("Edit", "MyController")
In order to get a URL for the API version of the controller (assuming you use the same controller name) you can use
#Url.Action("Edit", "api/MyController")
All the Url.Action method is doing is appending the root path of the application, with the controller name, followed by the action name (unless it is "Index" in which case it is not appended. if the route values object has an id property the value is also appended to the URL.