Webapi and normal methods in the same controller?









up vote
0
down vote

favorite












With the introduction of the Apicontroller attribute in asp.net core 2.1, I wonder how do I get the api and normal methods to work in the same controller.



[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase

[HttpPost]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)

//...


public async Task<IActionResult> CustomerOrders()

if (!User.IsInRole("Customer"))
return Challenge();
var customer = await _workContext.CurrentCustomer();

var model = await orderModelFactory.PrepareCustomerOrderListModel();
return View(model);




I can call post method /api/order/saveorder but cannot run the https://example.com/order/customerorders.




It shows an exceptions: InvalidOperationException: Action
'.CustomerOrders ' does not have an attribute route. Action methods on
controllers annotated with ApiControllerAttribute must be attribute
routed.




If I remove [ApiController] and [Route("api/[controller]")] on the controller level and instead put on the method level, then it surely works. still don't know if there's any better hybrid solution for these methods as i want to use this new ApiController feature.



[Route("/api/controller/saveorder")]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)


Any input is greatly appreciated.










share|improve this question























  • Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
    – Tetsuya Yamamoto
    yesterday










  • What do you mean by can not run? Does not hit? Return null? What do you expect to get?
    – ibubi
    yesterday











  • @ibubi sorry, i updated my post.
    – nam vo
    yesterday










  • @TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
    – nam vo
    yesterday






  • 2




    You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
    – Tetsuya Yamamoto
    yesterday














up vote
0
down vote

favorite












With the introduction of the Apicontroller attribute in asp.net core 2.1, I wonder how do I get the api and normal methods to work in the same controller.



[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase

[HttpPost]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)

//...


public async Task<IActionResult> CustomerOrders()

if (!User.IsInRole("Customer"))
return Challenge();
var customer = await _workContext.CurrentCustomer();

var model = await orderModelFactory.PrepareCustomerOrderListModel();
return View(model);




I can call post method /api/order/saveorder but cannot run the https://example.com/order/customerorders.




It shows an exceptions: InvalidOperationException: Action
'.CustomerOrders ' does not have an attribute route. Action methods on
controllers annotated with ApiControllerAttribute must be attribute
routed.




If I remove [ApiController] and [Route("api/[controller]")] on the controller level and instead put on the method level, then it surely works. still don't know if there's any better hybrid solution for these methods as i want to use this new ApiController feature.



[Route("/api/controller/saveorder")]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)


Any input is greatly appreciated.










share|improve this question























  • Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
    – Tetsuya Yamamoto
    yesterday










  • What do you mean by can not run? Does not hit? Return null? What do you expect to get?
    – ibubi
    yesterday











  • @ibubi sorry, i updated my post.
    – nam vo
    yesterday










  • @TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
    – nam vo
    yesterday






  • 2




    You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
    – Tetsuya Yamamoto
    yesterday












up vote
0
down vote

favorite









up vote
0
down vote

favorite











With the introduction of the Apicontroller attribute in asp.net core 2.1, I wonder how do I get the api and normal methods to work in the same controller.



[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase

[HttpPost]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)

//...


public async Task<IActionResult> CustomerOrders()

if (!User.IsInRole("Customer"))
return Challenge();
var customer = await _workContext.CurrentCustomer();

var model = await orderModelFactory.PrepareCustomerOrderListModel();
return View(model);




I can call post method /api/order/saveorder but cannot run the https://example.com/order/customerorders.




It shows an exceptions: InvalidOperationException: Action
'.CustomerOrders ' does not have an attribute route. Action methods on
controllers annotated with ApiControllerAttribute must be attribute
routed.




If I remove [ApiController] and [Route("api/[controller]")] on the controller level and instead put on the method level, then it surely works. still don't know if there's any better hybrid solution for these methods as i want to use this new ApiController feature.



[Route("/api/controller/saveorder")]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)


Any input is greatly appreciated.










share|improve this question















With the introduction of the Apicontroller attribute in asp.net core 2.1, I wonder how do I get the api and normal methods to work in the same controller.



[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase

[HttpPost]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)

//...


public async Task<IActionResult> CustomerOrders()

if (!User.IsInRole("Customer"))
return Challenge();
var customer = await _workContext.CurrentCustomer();

var model = await orderModelFactory.PrepareCustomerOrderListModel();
return View(model);




I can call post method /api/order/saveorder but cannot run the https://example.com/order/customerorders.




It shows an exceptions: InvalidOperationException: Action
'.CustomerOrders ' does not have an attribute route. Action methods on
controllers annotated with ApiControllerAttribute must be attribute
routed.




If I remove [ApiController] and [Route("api/[controller]")] on the controller level and instead put on the method level, then it surely works. still don't know if there's any better hybrid solution for these methods as i want to use this new ApiController feature.



[Route("/api/controller/saveorder")]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)


Any input is greatly appreciated.







c# asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 23 hours ago









Rob

908719




908719










asked yesterday









nam vo

1,24272958




1,24272958











  • Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
    – Tetsuya Yamamoto
    yesterday










  • What do you mean by can not run? Does not hit? Return null? What do you expect to get?
    – ibubi
    yesterday











  • @ibubi sorry, i updated my post.
    – nam vo
    yesterday










  • @TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
    – nam vo
    yesterday






  • 2




    You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
    – Tetsuya Yamamoto
    yesterday
















  • Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
    – Tetsuya Yamamoto
    yesterday










  • What do you mean by can not run? Does not hit? Return null? What do you expect to get?
    – ibubi
    yesterday











  • @ibubi sorry, i updated my post.
    – nam vo
    yesterday










  • @TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
    – nam vo
    yesterday






  • 2




    You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
    – Tetsuya Yamamoto
    yesterday















Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
– Tetsuya Yamamoto
yesterday




Hint: CustomerOrders is a HttpGet method, how you send the request for both methods and why the GET request doesn't work?
– Tetsuya Yamamoto
yesterday












What do you mean by can not run? Does not hit? Return null? What do you expect to get?
– ibubi
yesterday





What do you mean by can not run? Does not hit? Return null? What do you expect to get?
– ibubi
yesterday













@ibubi sorry, i updated my post.
– nam vo
yesterday




@ibubi sorry, i updated my post.
– nam vo
yesterday












@TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
– nam vo
yesterday




@TetsuyaYamamoto customerorder is not httpget, it's a nomarl action method to return view
– nam vo
yesterday




2




2




You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
– Tetsuya Yamamoto
yesterday




You cannot use return View() inside a controller marked with ApiControllerAttribute. You can only use either BadRequestResult, NotFoundResult, or OkObjectResult. What happened if you put [HttpGet] attribute without attribute routing provided?
– Tetsuya Yamamoto
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You are saying, that you cannot call https://example.com/order/customerorders. In your [Route("api/[controller]")] you define, that all Methods inside this controller will be available at https://example.com/api/order/.



So to call your method, you need to call https://example.com/api/order/customerorders.



If you want to stay with https://example.com/order/customerorders, you need to put the [Route] attributes at your methods:



[ApiController]
public class OrderController : ControllerBase

[HttpPost("api/order")]
public async Task<IActionResult> SaveOrder(SaveOrderModel model)

...



[HttpGet("order/customerorders")]
public async Task<IActionResult> CustomerOrders()

if (!User.IsInRole("Customer"))
return Challenge();
var customer = await _workContext.CurrentCustomer();

var model = await orderModelFactory.PrepareCustomerOrderListModel();
return View(model);







share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221671%2fwebapi-and-normal-methods-in-the-same-controller%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You are saying, that you cannot call https://example.com/order/customerorders. In your [Route("api/[controller]")] you define, that all Methods inside this controller will be available at https://example.com/api/order/.



    So to call your method, you need to call https://example.com/api/order/customerorders.



    If you want to stay with https://example.com/order/customerorders, you need to put the [Route] attributes at your methods:



    [ApiController]
    public class OrderController : ControllerBase

    [HttpPost("api/order")]
    public async Task<IActionResult> SaveOrder(SaveOrderModel model)

    ...



    [HttpGet("order/customerorders")]
    public async Task<IActionResult> CustomerOrders()

    if (!User.IsInRole("Customer"))
    return Challenge();
    var customer = await _workContext.CurrentCustomer();

    var model = await orderModelFactory.PrepareCustomerOrderListModel();
    return View(model);







    share|improve this answer
























      up vote
      1
      down vote



      accepted










      You are saying, that you cannot call https://example.com/order/customerorders. In your [Route("api/[controller]")] you define, that all Methods inside this controller will be available at https://example.com/api/order/.



      So to call your method, you need to call https://example.com/api/order/customerorders.



      If you want to stay with https://example.com/order/customerorders, you need to put the [Route] attributes at your methods:



      [ApiController]
      public class OrderController : ControllerBase

      [HttpPost("api/order")]
      public async Task<IActionResult> SaveOrder(SaveOrderModel model)

      ...



      [HttpGet("order/customerorders")]
      public async Task<IActionResult> CustomerOrders()

      if (!User.IsInRole("Customer"))
      return Challenge();
      var customer = await _workContext.CurrentCustomer();

      var model = await orderModelFactory.PrepareCustomerOrderListModel();
      return View(model);







      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You are saying, that you cannot call https://example.com/order/customerorders. In your [Route("api/[controller]")] you define, that all Methods inside this controller will be available at https://example.com/api/order/.



        So to call your method, you need to call https://example.com/api/order/customerorders.



        If you want to stay with https://example.com/order/customerorders, you need to put the [Route] attributes at your methods:



        [ApiController]
        public class OrderController : ControllerBase

        [HttpPost("api/order")]
        public async Task<IActionResult> SaveOrder(SaveOrderModel model)

        ...



        [HttpGet("order/customerorders")]
        public async Task<IActionResult> CustomerOrders()

        if (!User.IsInRole("Customer"))
        return Challenge();
        var customer = await _workContext.CurrentCustomer();

        var model = await orderModelFactory.PrepareCustomerOrderListModel();
        return View(model);







        share|improve this answer












        You are saying, that you cannot call https://example.com/order/customerorders. In your [Route("api/[controller]")] you define, that all Methods inside this controller will be available at https://example.com/api/order/.



        So to call your method, you need to call https://example.com/api/order/customerorders.



        If you want to stay with https://example.com/order/customerorders, you need to put the [Route] attributes at your methods:



        [ApiController]
        public class OrderController : ControllerBase

        [HttpPost("api/order")]
        public async Task<IActionResult> SaveOrder(SaveOrderModel model)

        ...



        [HttpGet("order/customerorders")]
        public async Task<IActionResult> CustomerOrders()

        if (!User.IsInRole("Customer"))
        return Challenge();
        var customer = await _workContext.CurrentCustomer();

        var model = await orderModelFactory.PrepareCustomerOrderListModel();
        return View(model);








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        wertzui

        2,05611831




        2,05611831



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221671%2fwebapi-and-normal-methods-in-the-same-controller%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

            Why am i infinitely getting the same tweet with the Twitter Search API?

            Solve: $(3xy-2ay^2)dx+(x^2-2axy)dy=0$