简体   繁体   English

同一控制器中的 Webapi 和普通方法?

[英]Webapi and normal methods in the same controller?

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.随着asp.net core 2.1中Apicontroller属性的引入,我想知道如何让api和普通方法在同一个控制器中工作。

[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 .我可以调用 post 方法/api/order/saveorder但不能运行https://example.com/order/customerorders

It shows an exceptions: InvalidOperationException: Action '.CustomerOrders ' does not have an attribute route.它显示了一个异常: InvalidOperationException: Action '.CustomerOrders ' 没有属性路由。 Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.使用 ApiControllerAttribute 注释的控制器上的操作方法必须进行属性路由。

If I remove [ApiController] and [Route("api/[controller]")] on the controller level and instead put on the method level, then it surely works.如果我在控制器级别删除 [ApiController] 和 [Route("api/[controller]")] 并将其放在方法级别,那么它肯定可以工作。 still don't know if there's any better hybrid solution for these methods as i want to use this new ApiController feature.仍然不知道这些方法是否有更好的混合解决方案,因为我想使用这个新的 ApiController 功能。

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

Any input is greatly appreciated.非常感谢任何输入。

You are saying, that you cannot call https://example.com/order/customerorders .您是说,您不能拨打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/ .在您定义的[Route("api/[controller]")] ,该控制器内的所有方法都将在https://example.com/api/order/上可用。

So to call your method, you need to call https://example.com/api/order/customerorders .因此,要调用您的方法,您需要调用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:如果您想继续使用https://example.com/order/customerorders ,则需要将[Route]属性放在您的方法中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM