I want to add the client name in each and every route in my .net core application. I do not want to add in each controller using Route OR RoutePrefix. It should be available for all controllers. So,how can I do this ?
Lets say I have following routes
mysite.com/virtualdir1/clientname/Login
mysite.com/virtualdir1/clientname/Dashboard
mysite.com/virtualdir1/clientname/Home
mysite.com/virtualdir1/clientname/AboutUs
I have tried to setup in startup.cs file as follows but it gives 404 error
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Backend",
template: "{clientname}/{controller=Home}/{action=Login}");
});
Thanks for the help !
Related
I am trying to make a website application with following structure of Views:
Views
Admin
Player
Index
Create
Update
Client
Index
and so on...
The issue is, how do I deal with routing since I have 2 subfolders in Views?
I tried to specify the route in the controller by:
return View("~/Views/Admin/Player/Index.cshtml");
and in the startup file:
{
routes.MapRoute(
name: "player",
template: "admin/Player"
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Sadly I cannot get to the address like https://localhost:5001/Admin/Player/ (returns a error 404)
but I get to the view by using address: https://localhost:5001/Player
Could somebody explain to me why it works like that? And how to get around it? I am tangled in that and cannot find my way out.
Thank you guys!
Your route for admin/Player doesnt specify what controller it will select. Your default route would work otherwise if you have a controller called AdminController with a method called Player.
I am working on ASP.NET CORE MVC project. Which has 2 routes:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
routes.MapRoute(
name: "news",
template: "News{controller}/{action}");
});
I noticed that in CORE 2.0 Framework the concept of defining specific
routes at first do not apply for my 2 routes shown above. Is there a Bug? or there something wrong with my code?
When the URL - '/NewsHome/Index' is fetched it is calling the Home Controller Action method.
It should actually call the NewsHome Controller's Index method. What is wrong ?
I am also giving an extract from the book - 'Pro ASP.NET Core MVC 2 by
Adam Freeman' to make my point. Please see below:
what is wrong, please guide me?
It's behaving exactly as explained in the book. Just invert the order of your routes.
The answer is in the screenshot you posted. You should reverse the order of the mappings. In the screenshot it's showing how it would be wrong.
change
routes.MapRoute(
name: "news",
template: "News{controller}/{action}");
TO
routes.MapRoute(
name: "news",
template: "/news/{action}");
I am beginner in ASP .NET Core 2.1 and working on project which is using ASP .NET Core 2.1 with individual authentication. I want to make my login page as my default route instead of Home/Index:
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
Any help how can i change it as ASP .NET Core 2.1 as Login is now used as a razor page instead of MVC Action View.
Use this in ConfigureServices method.
services.AddMvc().AddRazorPagesOptions(options=> {
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login","");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
then in Configure method
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
I solve this by using this code in ConfigureServices function (Startup.cs)
services.AddMvc().AddRazorPagesOptions(options => {
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/Account/Login");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
this may help, i haven't had a need to change default page myself
https://exceptionnotfound.net/setting-a-custom-default-page-in-asp-net-core-razor-pages/
Just use this in your configuration. This will add AuthorizeAttribute to your page
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/Home/Index");
});
Or change the Default route like this :
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Employees/Index", "");
});
See this page if necessary : https://learn.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.1
Insert this code to ConfigureServices() in Startup.cs
{
services.AddMvc().AddRazorPagesOptions(options =>
{
//Registering 'Page','route-name'
options.Conventions.AddPageRoute("/Account/Login", "");
});
}
Remember to remove any route name on "/Account/Login" Action declaration
After along time I solved it. Need add ALLOW for AREAS =>
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorPagesOptions(options =>
{
options.AllowAreas = true; //--working after add this line
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
});
Add authorization policy so that application by default asks user for authentication for the pages under abc folder and does not ask for some public pages under abc folder.
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/abc");
options.Conventions.AllowAnonymousToPage("/abc/PublicPage");
});
I had also in the same problem with my asp net core project which consists an API(asp net core web API) and a client(asp net core MVC), The problem I have faced when I published my project and tried to host it in my localhost it did not return me the login page which I have configured into the project startup.cs file the endpoint setting the default page I had set was "{controller=Account}/{action=Login}/{id?}" but localhost did not respond with this, after finding solution in the web I found some mechanism to do it but that solution doesn't suit me so I have made trick by my own and hope many net core developer would be helpful with my solution:
So the trick is I restore the previous configuration of end point as
"{controller=Home}/{action=Index}" and did a little change by removing "/{id?}" from the screen so the actual string was "
{controller=Home}/{action=Index}/{id?}
" and I have changed it to "{controller=Home}/{action=Index}" the logic is when program starts by default it will execute the default endpoint setting so program will take us to the controller which is "Home" and action is "Index" before returning the Index view I have checked a value "UserId" which must create upon login and kept it into a session variable,
var userid = _session.GetString("userid");
if (string.IsNullOrEmpty(userid))
{
return RedirectToAction("Login","Account");
}
This is how I solved my problem. The localhost and live publish also running fine, simply clicked view in browser and it returns the login page as my requirement.
Hope it helps.
Thanks
Edit: To be clear, I dont actually care if its the csproj. I just want to be able to globally define a root name for all my routes.
I am new to dotnet core 2 and c# in general. I am building a webapi template for the rest of my team to use, but I want to be able to put the csproj name as part of the route so people dont have to change it in every controller.
So for example:
WebStoreAbc.csproj
--------
[Route("api/v1/#CSPROJNAME#/products")]
public class ProductsController : Controller
{
... controller logic ...
}
Registers at runtime as:
/api/v1/WebStoreAbc/products
This way, I dont have to configure routes for a bunch of controllers everytime I make a new project from the template.
Edit 2: I used Mahmoud Heretani's answer below and tweaked it.
string projectName = "template";
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "api/v1/" + projectName.ToLower() + "/{controller=Home}/{action=Index}/{id?}");
});
}
It think that you cannot insert a variable inside [Route] attribute, because it required a constant value, however a simple trick inside your Configure method should do the trick:
var projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "api/v1/" + projectName + "/{controller=Home}/{action=Index}/{id?}");
});
But in your case, I would recommend that you create an assembly (class library) contains a method to configure the route using the assembly name (just like the previous example), and then use this method across your projects.
I'm using mvc beta6 and i want to have dynamic controller.
Right now i have created only two controller Home and Dynamic.
so default routing is controller/action and which was running fine till mvc 5
even we enter xyz/someaction in browser url.
But in case of mvc 6 it won't fire defaultcontroller factory and gives http error 404.because of that im not able to route to dynamic controller as user type in browser.
this is my start up code..
public void Configure(IApplicationBuilder app)
{
//app.UseMvc();
//// app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
/// routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
routes.MapRoute("defaultRoute",
"{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
}
I think routing is only registered for actual controller existed in app during compilation time.if i'm wrong then please tell me about the dynamic routing.
I've got the solution for my problem.The way of finding controller in mvc6 is very different from its previous versions.In mvc 6 all the controller types are discovered during compile time and cached through the DI framework and routing are registered to only those cached controller.So system doesnot invoke any factory methods unless it finds the requested controller. :)
That's it!
ps:thanks for everyone who really gave their valuable times.
i think you should try in following manner
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
You can also go with articles on my blog with "Routing in MVC"