将Route属性添加到ApiController

Van*_*nel 3 c# asp.net-mvc asp.net-web-api

我正在使用.NET Framework 4.0和C#开发ASP.NET Web Api应用程序.

我有这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MyProject.Web.Api.Controllers
{
    public class UserADController : ApiController
    {
        [HttpGet]
        [Route("api/UserAD/GetAllUsers")]
        public HttpResponseMessage GetAllUsers()
        {
            HttpResponseMessage response = null;


            return response;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

但是System.Web.Http.dll上没有RouteAttribute(这是在\packages\Microsoft.AspNet.WebApi.Core.4.0.30506.0\lib\net40\System.Web.Http.dll).

属性路由在ASP.NET MVC 5或更高版本以及ASP.NET Web API 2中是本机的.

此ASP.NET版本中此Route属性的等效值是多少?

ram*_*ilu 6

如果你想在ASP.Net WebAPI 1中使用基于属性的路由,那么安装这个nuget -

Install-Package AttributeRouting.WebApi
Run Code Online (Sandbox Code Playgroud)

然后装饰你的控制器动作 -

[GET("api/value")]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}
Run Code Online (Sandbox Code Playgroud)

然后运行解决方案 -

在此输入图像描述