Default route action not working after upgrade to ASP.NET Core MVC 2.2 - asp.net-core

After updating my app from asp.net core 2.1 to 2.2 it seems that default route action is not working for controllers that live in a separate class library.
For example I have a default route like this:
routes.MapRoute(
name: "default",
template: "{controller}/{action}"
,defaults: new { controller = "Home", action = "index" }
);
and in my class library I have a controller SiteAdminController which has an Index action method.
When I visit the url /siteadmin I get the HomeController index and not the index action of the SiteAdminController
if I use /siteadmin/index then it works
How can I make it work without requiring the index action to be explicitly in the url? It worked fine in 2.1

Have you tried setting the defaults in the template?
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
}
);
This worked for me in Core 2.2

Related

How do I configure routing for MVC and SignalR in an ASP.NET Core project?

I'm trying to build a web app with ASP.NET Core 2.1.0-preview1 and ASP.NET Core SignalR 1.0.0-alpha1 with an Angular4 client. So far I've been able to muddle through and can actually open a SignalR web socket connection from within the client. That came at a price, though, here's an excerpt of my Startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
/*routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });*/
});
app.UseWebSockets();
app.UseSignalR(routes =>
{
routes.MapHub<Feed>("feed");
});
As you can see, I had to disable the "spa-fallback" route, if I didn't do that SignalR would receive plain HTML instead of JSON. The drawback of that, of course, is that I have to enter the exact URL (localhost:12345/home/index, for example) now, which sucks.
So my question is, how can I make MVC routing and SignalR routing live side by side?
You need to put UseSignalR before UseMvc otherwise the fallback route will be registered before routes added by UseSignalR.

ASP.NET Core How to specify the route for the "example.com" and "example.com/" (home pages)

I updated my asp.net framework application to core. When my site was in .net framework the urls www.example.com and www.example.com/ worked. but now with core I get 404. I don't know why exactly it worked because in my .net framework RouteConfig.cs file I didn't specify any entry related to these routes it just worked and I didn't care now with core it doesn't work.
wwww.example.com/
www.example.com
In .net core Startup class I tried:
The code below gives 404.
routes.MapRoute(name: "default", template: "{controller}/{action}", defaults: new { controller = "Controller", action = "Home" });
The code below gives: The route template cannot start with a '/' or '~' character.
routes.MapRoute(name: "home", template: "/", defaults: new { controller = "Controller", action = "Home" } );
It doesn't work either.
routes.MapRoute(name: "home", template: "", defaults: new { controller = "Controller", action = "Home" } );
I know that the issue can be caused by web server configuration so I can update the question with additional information if needed.

Angular 2 and ASP.NET MVC 4 routing are not working together

I have implemented Angular2 in ASP.NET MVC 4 project. MVC routing is overriding Angular 2 routing. How can I handle both routing together?
I have done following modification:
_layout.cshtml:
<base href="/"/>
app.routing.ts:
const route: Routes= [
{path: 'firstpage', component: firstComponent},
{path: 'secondpage', component: secondComponent}
]
I have added below code above MVC default route in RouteConfig.cs:
routes.MapRoute(
name: "app1",
url: "app1/{*url}",
defaults: new { controller = "app1", action = "Index" }
);
I have tried using below urls:
http://localhost:50450/app1/firstpage
http://localhost:50450/#/app1/firstpage
Still is not working, what should I do or did I miss something?
the way I handled it is adding the same routes from the angular route in my RouteConfig like this:
routes.MapRoute(
name: "RouteName",
url: "routeUrl",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
so now if the route is http://localhost:3000/routeUrl the .net side will just forward the request over to home controller, index action and render it as if it were a request like this http://localhost:3000/ then angular 2 router can take over and do its thing.

Routes in MVC4 ASP.NET

I'm struggling with Routes in MVC4
I have Home as the controller and I have an Index. I believe a very typical set up! When I load the site from Visual Studio, it runs fine.
Now, I want to understand the routes. To do, I've tried to change the only route there is which is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
What I don't understand is why does this route not work. I only have 1 controller, which is Home, so by setting Home/{action}/ matches the pattern (although it obviously doesn't)
routes.MapRoute(
name: "Home",
url: "Home/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
Instead of getting the web page I get the following
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.
You are removing the default route, so there is no longer any route to handle the "/" route.
If you type in yoursite/Home or /Home/Index it will work. But since there is no longer any route to handle the default case, you get this issue.
What you want is to add an additional route, before your default route which does what you want. However, in this case it won't do anything different from the default route when you explicitly enter the /Home url. also, remove the trailing '/' off the url, as i'm not sure if that will cause problems. ie:
routes.MapRoute(
name: "Home",
url: "Home/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);

Single page application route in ASP.NET MVC 4

I'm building a single page application with asp.net net mvc4 and javascript.
My javascript has it's own routing engine, so I would like to have all routes under root handled by javascript, and all routes under api handled by server side. I.e.:
/ <--- javascript handles
/api <--- ASP.NET MVC handles
However, I don't have a clue of how to tell ASP.NET MVC to not handle these routes, and what I tried so far doesn't even run:
routes.MapRoute(
name: "Client",
url: "/",
defaults: new { controller = "Home", action = "Index", }
);
routes.MapRoute(
name: "API",
url: "api/{controller}/{action}/{id}",
defaults: new { controller = "Tasks", action = "Index", id = UrlParameter.Optional }
);
Any ideas are welcome, thanks!
You can't have defaults for parameters that don't exist in the route URL, unless you use a catch-all parameter. You'd also need to create your API route first to avoid it being consumed by the catch-all route.
routes.MapRoute(
name: "API",
url: "api/{controller}/{action}/{id}",
defaults: new { controller="Tasks", action="Index", id=UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{*catchall}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
update: I should mention that in this case, .NET MVC will still forward any requests not matching "API" on to "Home/Index," which in your case should serve up the entry point of your client-side MVC framework.