简体   繁体   中英

How to change the way REST Web API is called for ASP.NET

My controller currently looks like:

[Jsonp filter] 
public class ProductController : Controller
{

     public Json GetProduct(string id)
     {
          Product x;
          //code
          return Json(x, JsonRequestBehavior.AllowGet);
     }
 }

I am able to get a product doing this: api/product/getproduct/5

But, I want to be able to access it like this: api/product/5

What change do I need to make to do this?

EDIT: I am actually using Jsonp because I need to call this API from a different domain and get a json object back. Would this be possible using ApiController? Otherwise is there a way to do this without switching to ApiController?

You will have to edit your webapiconfig (located in the App_Start folder).

You will need to add something like this before any other route (to make sure it is caught first):

// Map Http Route takes in 3 parameters below
// param 1 is the name of the route.. This has nothing to do with class names or method names
// param 2 is the route itself.  Route parameters are denoted in curly braces {likethis}
// param 3 sets up defaults
config.Routes.MapHttpRoute("GetProductApi", "api/product/{id}",
    new {
        controller = "Product",      // the name of the controller class (without the Controller suffix)
        action = "GetProduct",       // the name of your method
        id = RouteParameter.Optional
    });

Also, your code for your controller looks like it isn't an API controller. Regardless, this is a routing problem. You can add a route configuration in your regular route config if you 100% need to.

Your code above is not a WebApi Controller it is an MVC Controller. Your class needs to inherits from ApiController instead like:

public class ProductController : ApiController{
...

Regarding your method I am not sure why you used Json as returned type since it is part of the MediaFormatter configuration to define the returned format, it should not be defined at method/function level. It looks like the correct method declaration will be something like:

public Product GetProduct(string id)
{
   Product x; //probably you want initialize it like new Product();       
   return x;
}

Update JsonP

WebApi works based on MediaFormatters as explained earlier. In order to use JsonP you need to use the proper media formatter there are several out there but how about:

http://www.nuget.org/packages/WebApi.JsonP

If you wish to read more about JsonP formatters for WebApi here is a SO Post about this:

您的ProductController应该派生自ApiController而不是Controller

Building on @Dalorzo's answer, if/when you can convert to an APIController, and if you can use WebAPI 2, you can use decorator attributes on your methods that will alter the routes and even the HTTP verbs to use for the method... which is really nice because everything you need to know about that API call is right there at the function signature. It's quite robust and intuitive, and I highly recommend it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM