Alaa Masalmeh
Alaa Masalmeh

Reputation: 67

ASP.Net WebApi change this default request method mapping with action name

I am Creating a new Web API Controller named Customer. and this Controller has one Action named "Create"

I couldn't make this Action able to be requested via "GET" HTTP Request in this form

http://ip:port/api/Customer/Create?userId=x&password=y

except in this method :

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    [ActionName("Create")]
    public MISApiResult<List<Branch>> GetCreate(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

Is there any other solution to preserve the action name as "Create" as next.

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    public MISApiResult<List<Branch>> Create(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

Thanks.

Edit:

Sorry for not being clear at the first time.

According to this answer:

How does a method in MVC WebApi map to an http verb?

There is a default http method according to the action names, if it starts with Get it'll bemapped to GET HTTP Method be default otherwise it will be mapped to POST.

Is there a way to change this default mapping with a custom one so I could map an action named "Create" with "GET" Http Method for testing purpose since this way is faster for development

I tried to put HttpGet Attribute and AcceptVerbs("GET") and it still map the action with POST Http method.

I found a way like I said and it's to change the action method name into GetCreate and then put ActionName attribute with "Create" value. but is there a way to change the default mapping?

Thanks again.

Upvotes: 0

Views: 2252

Answers (2)

Jins Peter
Jins Peter

Reputation: 2469

why dont you specify route. You actual issue is using System.Web.Mvc use System.Web.Http instead

 using System.Web.Http;
 [RoutePrefix("api/Customer")]
    public class CustomerController : ApiController
    {
        [HttpGet]
        [Route("Create")]
        public MISApiResult<List<Branch>> Create(string userID, string password)
        {
            return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
        }
    }

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

You can use custom route fore this action:

[HttpGet]
[Route("customer/create")]
public MISApiResult<List<Branch>> Create(string userID, string password)

Don't forget to enable attribute routing during application configuration (this should be added before default route definition):

config.MapHttpAttributeRoutes();

Though I would recommend to follow the conventions and use appropriate HTTP verbs - if you are creating a customer, then by convention you should use POST request to endpoint api/customers. Otherwise your API can be confusing for other people.

Also I would recommend to use IHttpActionResult as the return type of your method:

 public IHttpActionResult Post(string userID, string password)
 {
     if (_userRepository.Exists(userID))
         return BadRequest($"User with ID {userID} already exists");

     _userRepository.Create(userID, password);
     return StatusCode(HttpStatusCode.Created) // or CreatedAtRoute
 }

Further reading: Attribute Routing in ASP.NET Web API 2

Upvotes: 4

Related Questions