Asp.net Core Api Custom Routing
I want to access to my .net core controller action somewhat like this
api/myController/myAction
Tried the following and didn't work:
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
Any suggestions?
asp.net-core .net-core
add a comment |
I want to access to my .net core controller action somewhat like this
api/myController/myAction
Tried the following and didn't work:
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
Any suggestions?
asp.net-core .net-core
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work
– Chetan Sharma
Dec 28 '18 at 6:05
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40
add a comment |
I want to access to my .net core controller action somewhat like this
api/myController/myAction
Tried the following and didn't work:
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
Any suggestions?
asp.net-core .net-core
I want to access to my .net core controller action somewhat like this
api/myController/myAction
Tried the following and didn't work:
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
Any suggestions?
asp.net-core .net-core
asp.net-core .net-core
edited Dec 28 '18 at 7:00
Neville Nazerane
2,57621434
2,57621434
asked Dec 28 '18 at 5:49
Chetan SharmaChetan Sharma
525
525
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work
– Chetan Sharma
Dec 28 '18 at 6:05
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40
add a comment |
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work
– Chetan Sharma
Dec 28 '18 at 6:05
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work– Chetan Sharma
Dec 28 '18 at 6:05
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work– Chetan Sharma
Dec 28 '18 at 6:05
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40
add a comment |
1 Answer
1
active
oldest
votes
After spending some trial error. I found that we have to decorate that Action method Something like this:
[HttpGet]
[Route("[action]")]
public async Task<ActionResult<object>> myAction()`
And it worked.
I found out problem was if you don't route to action method as above it will take default action name as index and try to route it.
This Route parm forces it to look for action name ie myAction for this example. and you should have [Route("api/[controller]")]
decoration for your controller too.
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with[Route("[action]")]
When you do it will be solved.
– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"
– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer
– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
|
show 3 more comments
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',
autoActivateHeartbeat: false,
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53954187%2fasp-net-core-api-custom-routing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
After spending some trial error. I found that we have to decorate that Action method Something like this:
[HttpGet]
[Route("[action]")]
public async Task<ActionResult<object>> myAction()`
And it worked.
I found out problem was if you don't route to action method as above it will take default action name as index and try to route it.
This Route parm forces it to look for action name ie myAction for this example. and you should have [Route("api/[controller]")]
decoration for your controller too.
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with[Route("[action]")]
When you do it will be solved.
– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"
– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer
– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
|
show 3 more comments
After spending some trial error. I found that we have to decorate that Action method Something like this:
[HttpGet]
[Route("[action]")]
public async Task<ActionResult<object>> myAction()`
And it worked.
I found out problem was if you don't route to action method as above it will take default action name as index and try to route it.
This Route parm forces it to look for action name ie myAction for this example. and you should have [Route("api/[controller]")]
decoration for your controller too.
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with[Route("[action]")]
When you do it will be solved.
– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"
– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer
– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
|
show 3 more comments
After spending some trial error. I found that we have to decorate that Action method Something like this:
[HttpGet]
[Route("[action]")]
public async Task<ActionResult<object>> myAction()`
And it worked.
I found out problem was if you don't route to action method as above it will take default action name as index and try to route it.
This Route parm forces it to look for action name ie myAction for this example. and you should have [Route("api/[controller]")]
decoration for your controller too.
After spending some trial error. I found that we have to decorate that Action method Something like this:
[HttpGet]
[Route("[action]")]
public async Task<ActionResult<object>> myAction()`
And it worked.
I found out problem was if you don't route to action method as above it will take default action name as index and try to route it.
This Route parm forces it to look for action name ie myAction for this example. and you should have [Route("api/[controller]")]
decoration for your controller too.
edited Dec 29 '18 at 6:41
answered Dec 28 '18 at 6:43
Chetan SharmaChetan Sharma
525
525
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with[Route("[action]")]
When you do it will be solved.
– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"
– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer
– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
|
show 3 more comments
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with[Route("[action]")]
When you do it will be solved.
– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"
– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer
– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
this code alone does not help you achieve the above. you must have missed out something.
– Neville Nazerane
Dec 28 '18 at 7:03
The problem is it will take action=index if you don't decorate it with
[Route("[action]")]
When you do it will be solved.– Chetan Sharma
Dec 28 '18 at 7:16
The problem is it will take action=index if you don't decorate it with
[Route("[action]")]
When you do it will be solved.– Chetan Sharma
Dec 28 '18 at 7:16
the solution is still not valid. With what you have posted,
Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"– Neville Nazerane
Dec 28 '18 at 7:59
the solution is still not valid. With what you have posted,
Route
attribute would just override the configured default route, thus making the route simply "myAction" not "api/controller/myaction"– Neville Nazerane
Dec 28 '18 at 7:59
When you generate a controller by default it will be routed to following
[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer– Chetan Sharma
Dec 28 '18 at 8:13
When you generate a controller by default it will be routed to following
[Route("api/[controller]")] [ApiController] public class myControllerController : ControllerBase
all we need to add is what i mentioned above in answer– Chetan Sharma
Dec 28 '18 at 8:13
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
true, that is the missing part. the default route line is pointless in this context since you have completely overridden.
– Neville Nazerane
Dec 28 '18 at 8:14
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53954187%2fasp-net-core-api-custom-routing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Show some code what you have tried so far
– Nisfan
Dec 28 '18 at 5:50
app.UseMvc(routes => { routes.MapRoute("default", "{controller}/{action}/{id?}"); });
I tried This it didnt work– Chetan Sharma
Dec 28 '18 at 6:05
Add api before controller like "api/{controller}/{action}/{id?}"
– Nisfan
Dec 28 '18 at 6:28
It doesn't work either.. Any ways I got it solved after research..
– Chetan Sharma
Dec 28 '18 at 6:40