I have a web site in MVC4 with area "admin" inside controller named "HomeController" ,also in my project a folder controller with controller named "HomeController" : when I do a call to
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
I get error :
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
site1.co.il.Controllers.HomeController
site1.co.il.Areas.Admin.Controllers.HomeController
If you have the same controller and action in different areas, MVC4 has no way to choose one of them, unless you specify which is the desired route.
You can specify it like this:
return RedirectToAction("action", "controller", new { area = "area" });
In your case area should be "admin"
If you want to refer to the root Controller, area should be ""
You can also try to refer to the appropriate controller like this: "admin\home", but I don't know if the equivalent "\home" would work.
Related
I can't use RedirectToPage for any of the pages in the identity folder in my razor pages app.
If I use: return RedirectToPage("/Identity/Account/Manage/ChangePassword");
What I got is an error:
InvalidOperationException: No page named '/Identity/Account/Manage/ChangePassword' matches the supplied values.
In the case of using Redirect itself, routing works fine but I need to route some values so I have to use RedirectToPage
If you want to use RedirectToPage, try the below code:
return RedirectToPage("/Account/Manage/ChangePassword");
/Account/Manage/ChangePassword means that we are looking for the ChangePassword.cshtml file in the Manage folder in the Account folder of the Pages folder.
return RedirectToPage("/Privacy");
/Privacy means that we are looking for the Privacy.cshtml file in the Pages folder.
The RedirectToPage() method is referring to a Razor cshtml "page" rather than a html page in the browser. So it expects the parameter to refer to a cshtml page in your project.
update:
If you add in IndexModel class to call page in Indentity folder you can try :
public IActionResult OnGet()
{
return RedirectToPage("/Account/Manage/ChangePassword", new { area = "Identity" });
}
I want to load my application at http://localhost:52856/CRUD/Products/List so then i can start working on the List.cshtml file instead of loading Index.cshtml at http://localhost:52856 .
Look i know i can just put a button at the index file so then it can redirect to path or use the navbar, but personally i don't want to do that every single time.Just load the application at the file.cshtml i want. But How can i do it?
If you don't like my comment you could do the following in Index.cshtml.cs
public IActionResult OnGet()
{
return new RedirectToPageResult("/CRUD/Product/List");
}
1. Include MVC as a service into our app.
we can optionally use another method called AddRazorPagesOptions() like so:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Customer/Index", "");
});
Within that AddRazorPagesOptions() method, we can set things like route conventions and the root directory for pages. It turns out that, to set a default route for a page.
2. remove or rename Pages/Index.cshtml
I have a controller with Route attribute as well as every Action with it`s own Route like:
[Route("api/version/v1")]
public class MyController : ControllerBase
{
[Route("receipts/verifyReceipt")]
public IActionResult VerifyReceipt(...){....}
...... several actions with diffrent Routes
}
My aim is to have api route : 'api/version/v1/receipts/verifyReceipt'
How can I set prefix [Route("api/version/v1")] from config.json
I`ve tried to set it up from Startupt.cs
app.UseMvc(routes =>
{
routes.MapRoute( "default", apiCommon.Value);
});
where 'apiCommon.Value' is my prefix 'api/version/v1' While adding route attribute to MyController:
[Route("", Name = "default")]
But that seems to have no effect. And api route looks like this: '/receipts/verifyReceipt'
Any Ideas what I`am doing wrong?
I think UsePathBase() fits your needs best. It does routing after the pathbase use specified
call it before app.UseRouting()
app.UsePathBase(apiCommon.Value);
Found the solution for my problem on Global route prefixing in ASP.NET Core MVC
When using a .NET MVC to build a website, when do I need to include a new route's info in RouteConfig.cs?
I have seen there is one default route pre-configured and registered in the RouteConfig.cs file like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
When I created a new controller I saw that, without writing any route info in RouteConfig.cs, the corresponding view was rendered in my browser.
Even after creating a few more controllers, like testcontroller, that I accessed from test views, still without writing anything regarding a route for testcontroller, their views rendered properly.
As these views all render without editing anything in RouteConfig.cs, can you give me a few examples when I do need to write route info in RouteConfig.cs?
The RouteConfig exists for when you want to do a 'non default' route in MVC. As you can see, a lot of your ActionResults will be rendered by matching that route specified there. Ultimately you may be able to create an entire site and not even have to touch the RouteConfig.cs! It's only when you start to want different routes for use cases that you might find yourself diving in there.
An example for when you might need to edit it, is if you had an Area exclusively for blogs and wanted to do something along the lines of:
/Blog/Post/1234/my-blog-post
By default that wouldn't match the default config. In areas the default route config is prefixed by the Area name and then follows the usual standard like so.
/{area}/{controller}/{action}/{id}
In order to get override this we can write the following:
context.MapRoute(
"Blog",
"Blog/Post/{id}/{name}",
new { action = "Index",controller="Post" },
new { id = #"\d+" }
);
It's worth noting that in newer versions of MVC (5 onwards) we have access to the Routing attribute which is an easier and more accessible way to handle routing across controllers and actions.
Article: MVC5.1 Features, Attribute Routing..
Article: MVC5 Attribute Routing via David Hayden
Additional Reading for Routes in ASP.NET MVC
ASP.NET MVC Routing Overview
ASP.NET Typical Routing - URL Patterns
How to specify URL like DomainName/Admin/Folder/Controller/Action/Parameters in Asp.net MVC?Is It Require Routing?any one has any idea?please help me.
The routing framework is not filesystem based. It has no concept of what folder(s) the controller is in. You can get part of what you want via Areas, which allow you to group controllers under a common directory, but this is more than just a simple it directory. It has an actual class component that the routing framework uses in creating and interpreting the route. So for example, you could add an Area and then get a URL like:
/AreaName/ControllerName/ActionName/Parameters
But that's as far as you can take it.
Your only other option would be to use custom routes or attribute routing to "fake" the URLs you want. In other words, it still would have nothing to do with the actual filesystem path, but you could could define that the route should be prefixed with the static component of path you wanted.
routes.MapRoute(
name: "Default",
url: "Folder/SubFolder/{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
Or with attribute routing, you'd decorate your controller(s) with the RoutePrefix attribute:
[RoutePrefix("Folder/SubFolder/Foo")]
public class FooController : Controller
#Url.Action("Action", "Controller", new { area = "area", class="class", etc })
I Have faced same type of issue in my project. Specifying href for menus or for redirection fails when they keep that is folder subfolder.
Better option would be href with Url.action in html links.
<a class="nav-link" href="#Url.Action("ActionmethodName", "Controller Name", new { name = "John", contact = "calgary, vancouver" })">Home</a>
Use Full path Url for Ajax Calls:
specify full url for url param for ajax calls.