ASP.NET Core 2.2 Upgrade Routing from 2.1 to 2.2 - asp.net-core-2.2

I am upgrading my application from ASP.NET Core 2.1 to 2.2. There is a change in the routing that I am having problems with. I have reviewed the following links as a source.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2
https://blogs.msdn.microsoft.com/webdev/2018/08/27/asp-net-core-2-2-0-preview1-endpoint-routing/
Here is my Core 2.1 code in Startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//Area Marketing
routes.MapRoute(name: "Marketing",
template: "Marketing/{controller}/{action}/{id?}");
//Area Media
routes.MapRoute(name: "Media",
template: "Media/{controller}/{action}/{id?}");
//Area Membership
routes.MapRoute(name: "Membership",
template: "Membership/{controller}/{action}/{id?}");
//Area Military
routes.MapRoute(name: "Military",
template: "Military/{controller}/{action}/{id?}");
//Area Process
routes.MapRoute(name: "Process",
template: "Process/{controller}/{action}/{id?}");
});
Here is my Core 2.2 code in Startup.cs that I have migrated.
//ASP.NET Core 2.2 Routes
var routes = new RouteBuilder(app);
//
//routes.DefaultHandler = new MvcRouteHandler(???);
//
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//Area Marketing
routes.MapRoute(name: "Marketing",
template: "Marketing/{controller}/{action}/{id?}");
//Area Media
routes.MapRoute(name: "Media",
template: "Media/{controller}/{action}/{id?}");
//Area Membership
routes.MapRoute(name: "Membership",
template: "Membership/{controller}/{action}/{id?}");
//Area Military
routes.MapRoute(name: "Military",
template: "Military/{controller}/{action}/{id?}");
//Area Process
routes.MapRoute(name: "Process",
template: "Process/{controller}/{action}/{id?}");
app.UseRouter(routes.Build());
}
When I run the applicaiton I get the following error:
It looks like I need to create or setup a Handler for this. What would my Handler code look like as there are not any examples or tutorials that I can find.

Related

Vue 3 Go home on refresh

I have a vuetify (vue 3) app. When I (hard) refresh my web app, I'd like to be redirected to my very first route (for isntance, Skills). Instead I go to the last route I was on.
Here are my routes.
const routes = [
{
path: '/',
name: "Skills",
component: Skills,
beforeEnter: checkIfAuthenticated,
},
{
path: '/myskills',
name: "MySkills",
component: MySkills,
beforeEnter: checkIfAuthenticated,
},
{
path: '/history',
name: "History",
component: History,
beforeEnter: checkIfAuthenticated,
},
{
path: '/conversations',
name: "Conversations",
component: Conversations,
beforeEnter: checkIfAuthenticated,
},
{
path: '/:catchAll(.*)*',
name: "PageNotFound",
component: PageNotFound,
},
]
const router = createRouter({
history: createWebHistory(),
routes,
})
Thanks

Area Route Problem in Netcore 3.1, invalid routing

I have a problem with area
in netcore 3.1, I add my endpoint on startup.cs,
app.UseEndpoints(endpoints =>
{
// endpoints.MapControllers();
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
add the service
services.AddControllersWithViews();//.AddNewtonsoftJson(); ;
services.AddRazorPages();
but it's routing on this way, what obviously doesn't work
controller/action?area=area
Example
Patients/Create?area=Medical
This is the way than I call it (by legacy purpose)
<a class="btn btn-primary" href="#Url.Action("Create", "Patients", new { area = "Medicals" })">
New Patient</a>
I try in the classic way for netcore and trowme the same route
<a class="btn btn-primary" asp-action="Create" asp-controller="Patients" asp-area="Medicals">
New Patient</a>
I have the Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 3.1.9 Installed, than use to be the main problem with razor
The solution it's so stupid, the order for the area configuration it' important, so, area need to be set first
endpoints.MapControllerRoute(
name: "default",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

Asp.Net core MVC Area routing not working as expected

I have an application in which I want to use Area. But routing is not working as expected.
I have an Area called Admin
I have the following code in a startup.cs configure method
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapAreaControllerRoute(
name: "areas",
areaName: "Admin",
pattern: "{area}/{controller=Home}/{action=Index}/{id?}"
);
});
when I create an anchor tag using following code routing doesn't work
<a class="nav-link" asp-area="Admin" asp-controller="Home" asp-action="Index">Admin</a>
it gives me following url when the application runs
https://localhost:44321/?area=Admin
what is the issue? why proper url is not generated
This may help you. I use this for my .net core 3.1 project and it work well, without any troubles.
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "AdministratorArea",
areaName: "Administrator",
pattern: "Administrator/{controller=Home}/{action=Index}"); //This is Administrator route. You can you {id} and other parameters which you want
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}"); // This route is for Controllers which are situated in project controller folder
});
You also got the right code.. Just make sure your new AreaControllerRoute is up above the default one.
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "areas",
areaName: "Admin",
pattern: "{area}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});

SPA MVC Routing Mirroring no Controller in URL

I like to route routes directly to the index controller. I do this to ge 404 responses for SEO. How can i route the controller with a different url name
// Map to www.mydomain.com
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: indexCtrl);
// Map to www.mydomain.com/services
routes.MapRoute(
name: "Services",
url: "services",
defaults: indexCtrl);

Get a routing parameter in Twig template (which is not in query)?

Is it possible to get a parameter in a Twig template which is not in the query without having to pass it through the controller ?
The idea would be to access the section value from a routing like this one (to avoid long Twig conditions to add "active" CSS classes in template menus) :
ibcpg_admin_dashboard:
path: /
defaults: { _controller: IBCPGAdministrationBundle:Main:dashboard, section: dashboard }
ibcpg_admin_courses:
path: /courses
defaults: { _controller: IBCPGAdministrationBundle:Course:list, section: courses }
ibcpg_admin_course_add:
path: /course/add
defaults: { _controller: IBCPGAdministrationBundle:Course:add, section: courses }
ibcpg_admin_course_edit:
path: /course/edit/{id}
defaults: { _controller: IBCPGAdministrationBundle:Course:edit, section: courses }
requirements:
id: \d+
ibcpg_admin_course_delete:
path: /course/delete/{id}
defaults: { _controller: IBCPGAdministrationBundle:Course:delete, section: courses }
requirements:
id: \d+
ibcpg_admin_course_chapters:
path: /course-chapters
defaults: { _controller: IBCPGAdministrationBundle:CourseChapter:listAndAdd, section: course_chapters }
I discovered that all parameters defined directly within defaults table (in routing) are filling the attributes part of the ParameterBag, so you can access them like this in Twig templates :
{{ app.request.attributes.get('my_parameter') }}
my_parameter would be replace by section for the example I gave.

Resources