0

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.

7
  • 3
    You are creating a customer. How is it Get request. It needs to be a post request Commented Jun 6, 2017 at 13:34
  • can you tell us why you need Create as action Name name and Get as HttpMethod.? Do you have a justification? Commented Jun 7, 2017 at 8:25
  • it's faster for testing the Action URL in development phase. I'll change it back later to POST when it's release. Commented Jun 7, 2017 at 8:31
  • So. it is about action URL right, that why we have [RoutePrefix("Customer")] for the controller and [Route("Create")] Commented Jun 7, 2017 at 8:34
  • Also, how come you have to accept both GET and post in Same action method. I dont think thats a good practice at all. you can have same URL for two action methods which gets and posts respectively. Try to do best practices in development phase as well. Your profile seem to have 8 year experience. I cant find that from your question. Commented Jun 7, 2017 at 8:42

2 Answers 2

4

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

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

Comments

2

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

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.