3

I'm developing an ASP.NET Web Api app with .NET Framework 4.0 and C#.

I have this:

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;
        }

    }
}

But there isn't a RouteAttribute on System.Web.Http.dll (this is in \packages\Microsoft.AspNet.WebApi.Core.4.0.30506.0\lib\net40\System.Web.Http.dll).

Attribute Routing is native in ASP.NET MVC 5, or later, and ASP.NET Web API 2.

What is the equivalent for this Route attribute in this ASP.NET version?

5
  • what you mean by "there isn't a RouteAttribute on System.Web.Http.dll" ? EDIT: maybe duplicated with stackoverflow.com/questions/29841993/… Commented Apr 29, 2015 at 12:22
  • and be sure you got the proper routing configuration enabled this way: dotnet.microsoft.com/en-us/apps/aspnet [asp.net link] Commented Apr 29, 2015 at 12:29
  • 1
    You really want to move to Web API 2.3.3 if you can. Commented Apr 29, 2015 at 18:15
  • @YishaiGalatzer Yes, I know and I want to move to it but in my company they don't want :(. Commented Apr 30, 2015 at 6:02
  • Let them know that WebAPI 2.3.3 is compatible with WebAPI 1.0 and has numerous bug fixes performance improvements and other enhancements over the new features. Notice that all bug fixes (other than security fixes) are only applied to the latest version, so you have round about 8 RTM releases between 1.0.0 to 2.3.3 with tons of bug fixes. Commented Apr 30, 2015 at 19:12

1 Answer 1

6

If you want Attribute Based routing in ASP.Net WebAPI 1, then install this nuget -

Install-Package AttributeRouting.WebApi

Then decorate your controller action -

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

Then run the solution -

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.