分类目录归档:Web API

.net rest api 下载文件代码

[HttpGet]
public HttpResponseMessage GetImage()
{
  //文件路径
  var path = HostingEnvironment.MapPath("~/App_Data/5.jpg");
  FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
  Image img = Image.FromStream(fileStream);
  MemoryStream ms = new MemoryStream();
  img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
  result.Content = new ByteArrayContent(ms.ToArray());
  result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
  result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  {
      FileName = "5.jpg"//文件名
  };
  return result;
}

Add jsonp support for your .net mvc api project

使用Attribute来实现jsonp支持

public class JsonpAttribute : ActionFilterAttribute
{
  private const string CallbackQueryParameter = "callback";
  public override void OnActionExecuted(HttpActionExecutedContext context)
  {
    var callback = string.Empty;
    if (IsJsonp(out callback))
    {
      var jsonBuilder = new StringBuilder(callback);
      jsonBuilder.AppendFormat("({0})", context.Response.Content.ReadAsStringAsync().Result);
      context.Response.Content = new StringContent(jsonBuilder.ToString());
    }
    base.OnActionExecuted(context);
  }
  private bool IsJsonp(out string callback)
  {
    callback = HttpContext.Current.Request.QueryString[CallbackQueryParameter];
    return !string.IsNullOrEmpty(callback);
  }
}

然后在你的WebApiConfig里面加上
config.Filters.Add(new JsonpAttribute());
这样你的api既能支持json,又能支持jsonp了。
如果url里有?callback=?,就会返回jsonp格式的数据,如果没有,依然是json

如何让ASP.NET WEB API 默认返回JSON 格式

Web API 默认是返回JSON和XML的,在WebApiConfig的Register方法里可以去掉XML formatter

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    //remove xml, default json
    config.Formatters.Remove(config.Formatters.XmlFormatter);
  }
}

Web API:Handling Circular Object References

By default, the JSON and XML formatters write all objects as values. If two properties refer to the same object, or if the same object appears twice in a collection, the formatter will serialize the object twice. This is a particular problem if your object graph contains cycles, because the serializer will throw an exception when it detects a loop in the graph.
Consider the following object models and controller.

public class Employee
{
    public string Name { get; set; }
    public Department Department { get; set; }
}
public class Department
{
    public string Name { get; set; }
    public Employee Manager { get; set; }
}
public class DepartmentsController : ApiController
{
    public Department Get(int id)
    {
        Department sales = new Department() { Name = "Sales" };
        Employee alice = new Employee() { Name = "Alice", Department = sales };
        sales.Manager = alice;
        return sales;
    }
}

Invoking this action will cause the formatter to thrown an exception, which translates to a status code 500 (Internal Server Error) response to the client.
To preserve object references in JSON, add the following code to?Application_Start?method in the Global.asax file:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
    Newtonsoft.Json.PreserveReferencesHandling.All;

Now the controller action will return JSON that looks like this:

{"$id":"1","Name":"Sales","Manager":{"$id":"2","Name":"Alice","Department":{"$ref":"1"}}}

Notice that the serializer adds an “$id” property to both objects. Also, it detects that the Employee.Department? property creates a loop, so it replaces the value with an object reference: {“$ref”:”1″}.
Object references are not standard in JSON. Before using this feature, consider whether your clients will be able to parse the results. It might be better simply to remove cycles from the graph. For example, the link from Employee back to Department is not really needed in this example.
To preserve object references in XML, you have two options. The simpler option is to add[DataContract(IsReference=true)]?to your model class. The?IsReference?parameter enables oibject references. Remember that?DataContract?makes serialization opt-in, so you will also need to add?DataMember?attributes to the properties:

[DataContract(IsReference=true)]
public class Department
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public Employee Manager { get; set; }
}

Now the formatter will produce XML similar to following:

<Department xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1"
            xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
            xmlns="http://schemas.datacontract.org/2004/07/Models">
  <Manager>
    <Department z:Ref="i1" />
    <Name>Alice</Name>
  </Manager>
  <Name>Sales</Name>
</Department>

If you want to avoid attributes on your model class, there is another option: Create a new type-specificDataContractSerializer?instance and set?preserveObjectReferences?to?true?in the constructor. Then set this instance as a per-type serializer on the XML media-type formatter. The following code show how to do this:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
var dcs = new DataContractSerializer(typeof(Department), null, int.MaxValue,
    false, /* preserveObjectReferences: */ true, null);
xml.SetSerializer<Department>(dcs);

Routing in ASP.NET Web API

Routing Tables

In ASP.NET Web API, a?controller?is a class that handles HTTP requests. The public methods of the controller are called?action methods?or simply?actions. When the Web API framework receives a request, it routes the request to an action.
To determine which action to invoke, the framework uses a?routing table. The Visual Studio project template for Web API creates a default route:

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

This route is defined in the WebApiConfig.cs file, which is placed in the App_Start directory:

For more information aboout the?WebApiConfig?class, see?Configuring ASP.NET Web API?.
If you self-host Web API, you must set the routing table directly on the?HttpSelfHostConfiguration?object. For more information, see?Self-Host a Web API.
Each entry in the routing table contains a?route template. The default route template for Web API is “api/{controller}/{id}”. In this template, “api” is a literal path segment, and {controller} and {id} are placeholder variables.
When the Web API framework receives an HTTP request, it tries to match the URI against one of the route templates in the routing table. If no route matches, the client receives a 404 error. For example, the following URIs match the default route:

  • /api/contacts
  • /api/contacts/1
  • /api/products/gizmo1

However, the following URI does not match, because it lacks the “api” segment:

  • /contacts/1

Note:?The reason for using “api” in the route is to avoid collisions with ASP.NET MVC routing. That way, you can have “/contacts” go to an MVC controller, and “/api/contacts” go to a Web API controller. Of course, if you don’t like this convention, you can change the default route table.
Once a matching route is found, Web API selects the controller and the action:

  • To find the controller, Web API adds “Controller” to the value of the?{controller}?variable.
  • To find the action, Web API looks at the HTTP method, and then looks for an action whose name begins with that HTTP method name. For example, with a GET request, Web API looks for an action that starts with “Get…”, such as “GetContact” or “GetAllContacts”.? This convention applies only to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller. We’ll see an example of that later.
  • Other placeholder variables in the route template, such as?{id},?are mapped to action parameters.

Let’s look at an example. Suppose that you define the following controller:

public class ProductsController : ApiController
{
    public void GetAllProducts() { }
    public IEnumerable<Product> GetProductById(int id) { }
    public HttpResponseMessage DeleteProduct(int id){ }
}

Here are some possible HTTP requests, along with the action that gets invoked for each:

HTTP Method URI Path Action Parameter
GET api/products GetAllProducts (none)
GET api/products/4 GetProductById 4
DELETE api/products/4 DeleteProduct 4
POST api/products (no match)

Notice that the?{id}?segment of the URI, if present, is mapped to the?id?parameter of the action. In this example, the controller defines two GET methods, one with an?id?parameter and one with no parameters.
Also, note that the POST request will fail, because the controller does not define a “Post…” method.

Routing Variations

The previous section described the basic routing mechanism for ASP.NET Web API. This section describes some variations.

HTTP Methods

Instead of using the naming convention for HTTP methods, you can explicitly specify the HTTP method for an action by decorating the action method with the?HttpGet,?HttpPut,?HttpPost, or?HttpDelete?attribute.
In the following example, the FindProduct method is mapped to GET requests:

public class ProductsController : ApiController
{
    [HttpGet]
    public Product FindProduct(id) {}
}

To allow multiple HTTP methods for an action, or to allow HTTP methods other than GET, PUT, POST, and DELETE, use the?AcceptVerbs?attribute, which takes a list of HTTP methods.

public class ProductsController : ApiController
{
    [AcceptVerbs("GET", "HEAD")]
    public Product FindProduct(id) { }
    // WebDAV method
    [AcceptVerbs("MKCOL")]
    public void MakeCollection() { }
}

Routing by Action Name

With the default routing template, Web API uses the HTTP method to select the action. However, you can also create a route where the action name is included in the URI:

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

In this route template, the?{action}?parameter names the action method on the controller. With this style of routing, use attributes to specify the allowed HTTP methods. For example, suppose your controller has the following method:

public class ProductsController : ApiController
{
    [HttpGet]
    public string Details(int id);
}

In this case, a GET request for “api/products/details/1” would map to the Details method. This style of routing is similar to ASP.NET MVC, and may be appropriate for an RPC-style API.
You can override the action name by using the?ActionName?attribute. In the following example, there are two actions that map to “api/products/thumbnail/id. One supports GET and the other supports POST:

public class ProductsController : ApiController
{
    [HttpGet]
    [ActionName("Thumbnail")]
    public HttpResponseMessage GetThumbnailImage(int id);
    [HttpPost]
    [ActionName("Thumbnail")]
    public void AddThumbnailImage(int id);
}

Non-Actions

To prevent a method from getting invoked as an action, use the?NonAction?attribute. This signals to the framework that the method is not an action, even if it would otherwise match the routing rules.

// Not an action method.
[NonAction]
public string GetPrivateData() { ... }

Further Reading

This topic provided a high-level view of routing. For more detail, see?Routing and Action Selection, which describes exactly how the framework matches a URI to a route, selects a controller, and then selects the action to invoke.