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

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.

Related

How to Default Route in ASP.NET Core 6 MVC?

I'm trying to learn ASP.NET Core 6, and now following a tutorial using ASP.NET Core 2. After adding the controller phase they delete this line from the startup file
app.run(async (context) = await context.response.writeasync("hello world"))
and add
app.UseMvcWithDefaultRoute();
so they can see the views.
As you know there is no startup file in ASP.NET Core 6, and I don't know what to do at this point.
I searched the internet a lot and have nothing.
I try to adjust the code in the program file like that
app.MapGet("/", () => app.UseMvcWithDefaultRoute);
but that does not work.
In asp.net core6 we define the default route like this:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
You can read Routing to controller actions in ASP.NET Core to know more.

aspnet core get configured route templates

is it possibile retrive the endpoint templates configured on the Configure method in the StartUp.cs ?
app.UseEndpoints(endpoints =>
{
endpoints.MapDynamicControllerRoute<TranslationTransformer>("{tenant}/{slug}");
endpoints.MapDynamicControllerRoute<TranslationTransformer>("{tenant}/{typology}/{slug}/{id}");
});
I try to export the template section for passing passing it to an /routing api
Thanks in advance.

Default route action not working after upgrade to ASP.NET Core MVC 2.2

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

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.

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.