csharp 常见的webapi配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 常见的webapi配置相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Newtonsoft.Json;
using System.Web.Routing;
using System.Net.Http;

namespace WebRoleV2
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
            json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                constraints: new { id = @"\d+" }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApiWithGuid",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                constraints: new { id = @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b" }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApiWithAction",
                routeTemplate: "api/{controller}/{action}"
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApiWithLevel",
                routeTemplate: "api/{controller}/{action}/{subaction}",
                defaults: new { subaction = RouteParameter.Optional },
                constraints: new { subaction = @"\w+" }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApiGet",
                routeTemplate: "Api/{controller}",
                defaults: new { action = "Get" },
                constraints: new { httpMethod = new HttpMethodConstraint("Get") }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApiPost",
                routeTemplate: "Api/{controller}",
                defaults: new { action = "Post" },
                constraints: new { httpMethod = new HttpMethodConstraint("Post") }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApiPut",
                routeTemplate: "Api/{controller}",
                defaults: new { action = "Put" },
                constraints: new { httpMethod = new HttpMethodConstraint("Put") }
            );

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();
        }
    }
}

以上是关于csharp 常见的webapi配置的主要内容,如果未能解决你的问题,请参考以下文章

csharp 自动WebAPI lowerCase序列化。

csharp RouteTestingExtensions仅限WebAPI

webapi权限常见错误

csharp 用于测试WebApi动作过滤器的助手类

csharp 如何使用WebApi获取Image节点的alt文本

csharp #c##webapi klient do mediqusa