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.
c# asp.net-core
add a comment |
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.
c# asp.net-core
Hint:CustomerOrders
is aHttpGet
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? Returnnull
? 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 usereturn View()
inside a controller marked withApiControllerAttribute
. You can only use eitherBadRequestResult
,NotFoundResult
, orOkObjectResult
. What happened if you put[HttpGet]
attribute without attribute routing provided?
– Tetsuya Yamamoto
yesterday
add a comment |
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.
c# asp.net-core
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
c# asp.net-core
edited 23 hours ago
Rob
908719
908719
asked yesterday
nam vo
1,24272958
1,24272958
Hint:CustomerOrders
is aHttpGet
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? Returnnull
? 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 usereturn View()
inside a controller marked withApiControllerAttribute
. You can only use eitherBadRequestResult
,NotFoundResult
, orOkObjectResult
. What happened if you put[HttpGet]
attribute without attribute routing provided?
– Tetsuya Yamamoto
yesterday
add a comment |
Hint:CustomerOrders
is aHttpGet
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? Returnnull
? 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 usereturn View()
inside a controller marked withApiControllerAttribute
. You can only use eitherBadRequestResult
,NotFoundResult
, orOkObjectResult
. 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
add a comment |
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);
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered yesterday
wertzui
2,05611831
2,05611831
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Hint:
CustomerOrders
is aHttpGet
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 withApiControllerAttribute
. You can only use eitherBadRequestResult
,NotFoundResult
, orOkObjectResult
. What happened if you put[HttpGet]
attribute without attribute routing provided?– Tetsuya Yamamoto
yesterday