C
C#2y ago
reeeeeee

❔ Remove Index from URL

How do I remove "Index" from Url, so I can call /Home, /Users, etc, wihtout additional /index. (but obv be still redirected to "Index" method in controller. I found this snipped and added it to my startup
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
Well, the Index is still there, and all actions without "Index" are redirected to my Users/edit/{id} action..
14 Replies
jalepi
jalepi2y ago
Without the rest of the code it's difficult to tell
reeeeeee
reeeeeeeOP2y ago
Which part would be helpful? Controllers?
jalepi
jalepi2y ago
Controller, yes
reeeeeee
reeeeeeeOP2y ago
Home
public async Task<IActionResult> Index()
{
_logger.Info("Index was clicked!");
return View();
}
Home
public async Task<IActionResult> Index()
{
_logger.Info("Index was clicked!");
return View();
}
User
[MvcAuthorize]
public async Task<IActionResult> Index(string search, int? page, int? pageSize)
{
... return View(model); }

[MvcAuthorize]
[HttpGet("{id}")]
public async Task<IActionResult> Edit(string id) {
... }
User
[MvcAuthorize]
public async Task<IActionResult> Index(string search, int? page, int? pageSize)
{
... return View(model); }

[MvcAuthorize]
[HttpGet("{id}")]
public async Task<IActionResult> Edit(string id) {
... }
Container
public async Task<IActionResult> Index() {....}
Container
public async Task<IActionResult> Index() {....}
Should I specify [Route] for each method?
jalepi
jalepi2y ago
You definitely can, but easier should be mapping a route without {action} or setting it as default equals to Index.
reeeeeee
reeeeeeeOP2y ago
But this be something like:
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}"
);
The localhost/ shows like /Home/Index the /Home shows like /User/Edit The /Container shows like /User/Edit I dont get it .. hm, could this be an issue with my Users.Edit?
[MvcAuthorize]
[HttpGet("{id}")]
public async Task<IActionResult> Edit(string id) {
[MvcAuthorize]
[HttpGet("{id}")]
public async Task<IActionResult> Edit(string id) {
jalepi
jalepi2y ago
Try commenting it out and see what happens
reeeeeee
reeeeeeeOP2y ago
Yeah, if I remove it everything works fine hmm
jalepi
jalepi2y ago
There is something wrong with edit's httpget attribute. I don't think is necessary. Try using just [HttpGet] instead, since {id} is set in defaul route...
reeeeeee
reeeeeeeOP2y ago
yeah i removed the [httpGet ] and it works fine, gonna keep it like this for now
jalepi
jalepi2y ago
It is apparently adding a new route
reeeeeee
reeeeeeeOP2y ago
thanks for your help 😄 yeah i guess it matched too many routes
jalepi
jalepi2y ago
rubberduck everything is going there 😂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server