This type of url works: ABC/CSDS?id=314
Folder Structure:
Controllers - Folder
,ABCController - Class
,CSDS = Action
Global.cs file has:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
How to make change for subdirectory url like
ABC/TESTN/CSDS?id=314 to go to
Controllers - Folder
,ABCController - Class
,CSDS = Action
Currently it says "The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable"
How about adding a rule like this:
routes.MapRoute(
"Default", // Route name
"{controller}/TESTN/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Warning, not tested, I don't have the tools installed at work :(
Related
Given this URL's
http://localhost:51095/Person // This is equivalent to this one Person/Index
http://localhost:51095/Person/Allan
I setup a route config for it as follows :
routes.MapRoute(
"Person",
"Person/{personName}",
new { controller = "Person", action = "Person", personName = UrlParameter.Optional }
)
;
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
First URL should follow the Default route and the second should follow the Person route.
This is not working because the first config seems to catch all these URL's
The first thing I'd do is remove personName = UrlParameter.Optional in the first Route. This would allow only urls that provide a personName value to access this Route. If no value is provided, it should fall through to the default Route.
But you'd want to think about the future with this strategy: if you implement new actions on that Person controller, you'd need to add a new Route for each of them. If you had a new 'Edit' action for example:
routes.MapRoute(
"Person_Edit",
"Person/Edit/{personName}",
new { controller = "Person", action = "Edit" }
)
You'd want to add these new Routes before the first one though - ordering/precedence of Routes is important.
This is related my question which i asked in this link correct me on url routing in mvc
Now i came with another problem, so i thought i will ask it as new question.
Now i have following routes in my global.asax file
routes.MapRoute(
"Custom", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
and
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
Now what happens is when i run my solution the URL i am getting is http://localhost:65423/Login this is what i need for my Login Page that is OK. But when i login in as user i am getting "The resource cannot be found" error.
when i checked it i can see that my URL is now changed to http://localhost:65423/Admin/Dashboard
So i think this causing the issue. So this looks the problem related to my global.asax routing.
Can anyone help me to find out what i did wrong.
You have 2 routes with completely optional segments. The issue is that there is no way for the routing framework to differentiate between them.
The only way you can make it work with your existing routes is to specify them explicitly by name (such as when using #Html.RouteLink or #Html.RouteUrl).
#Html.RouteLink("Custom Link 1", "Custom", new { action = "BigClientLogin" })
#Html.RouteLink("Custom Link 2", "Custom", new { action = "Action2" })
#Html.RouteLink("Home Page", "Default", new { controller = "Home", action = "Index" })
#Html.RouteLink("About", "Default", new { controller = "Home", action = "About" })
Doing it that way will function, but is not normal. Typically, there is only one route configured with all defaults for the controller, action, and id and the rest have some explicitly declared segments and/or constraints (segments being preferable).
routes.MapRoute(
"Custom", // Route name
"Custom/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
The first route will now only match when the URL starts with /Custom/. If it does not start with custom, it will match the default route.
The trick is to ensure that the routes are listed in the right order and that they only match the URL in specific cases, letting them pass on to the next route in the list if the case is not correct.
It happens because of 2 things.
1st the sequence of Your route
2nd your defaults
So if you end up on 'admin/dashboard' that's not a default, guess you've just put it in your studio to start there?
To get to http://localhost:65423/Login you'll need an action on Your AuthenticationController called 'Login' but it looks like the one You have configured is 'BicClientLogin' so You won't be taken to login unless you specify it, and at that time it has to exist.
To Help You further we need to know what controllers You have and what actions there exists, plus if security is part of your solution and if in case, what is is set to use.
I have a MVC 4 WebSite with several Areas... I´m using all default routing created by VS2012...
So, I can access (from Area1) :
Area1/ControllerX/ActionX
I have some controllers without Area, so I can access :
ControllerY/ActionY
All fine... But, if I try to access the ControllerX without the Area1, like that :
ControllerX/ActionX
I got that error:
Exception: The view 'ActionX' or its master was not found or no view engine supports the
searched locations. The following locations were searched: ~/Views/mangavagao/ActionX.cshtml
~/Views/Shared/ActionX.cshtml
Controller: ControllerX
Action: ActionX
I was expecting a Not Found 404 error... Why is that route been captured ?
--
Area route:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional }
);
Default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ControllerY", action = "ActionY", id = UrlParameter.Optional );
Add the namespaces parameter in the default maproute function. Then set the UseNamespaceFallback datatoken to false.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MvcApplication.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
namespaces parameter is set to prioritize the controller finding, when multiple controller with the same name exists.
MVC will still search for controller outside of this namespace, if no match is found in the namespace.
UseNamespaceFallback datatoken tells MVC to disregard the (2) statement.
hope this helps.
Try to map the area route with a namespace:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional },
new[] { "App.Areas.AreaName.Controllers" }
);
Change App and AreaName to the corresponding values.
This is similar to this question: Not including area name in URL results in "The view 'Index' or its master was not found" instead of 404
In your case the namespaces need to be added to the default route, not the area route. The namespace in this case should not reference the area controllers.
Something like this should work:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
null, // object constraints
new string[] { "Namespace.Application.Controllers" } // namespaces
);
related:
Multiple types were found that match the controller named 'Home'
Multiple types were found that match the controller named 'Home' - In two different Areas
Multiple types were found that match the controller named 'FW'.
The request for 'FW' has found the following matching controllers:
app.Controllers.Admin.FWController
app.Areas.Manage.Controllers.FWController
I tried the suggestions from those related links. I attempted to differentiate the controllers by using different namespaces:
global.asax.cs
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
routes.MapRoute(
"Default_Admin_Top", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers.Admin" }
);
in the manage area ManageAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Manage_default",
"Manage/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "app.Areas.Manage.Controllers" }
);
}
I also looked in the bin folder for an old version but there was only the current one.
What am I missing? It seems like this should work.
The issue appears to revolve around the fact that I gave my controller its own namespace without it being in its own area:
namespace app.Controllers.Admin
{
public class FWController : Controller{}
}
Removing the .Admin from the namespace here will remove the collision and also the error, but I do not fully understand why.
can you try and rearrage your rotes like this. The problem might be arising because app.Controllers.Admin lies in app.Controller
routes.MapRoute(
"Default_Admin_Top", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers.Admin" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
it looks similar to an issue which i have faced lately.
according to your scenario and having experienced the same issue, i suspect the problem would be the naming convention with controllers. Say for instance, your application has a controller named FWController in your root structure before re-structuring/maintaining your code in separate areas. when your application starts, the Application_Start() method inside Global.asax file registers route to route collection table (one of the core features of MVC 3 pattern) which maintain your incoming request when an action method executes inside a controller.
problem arises when you have a controller with same name like in your case FWController and directory structure of area somehow mimics the root directory just as you see in one of the referenced urls.
I implemented the similar solution as one of the URL justifies, i.e.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, //Parameter defaults
new []{"MyProject.WWW.Controllers"}
);
i did not touch my area registration.cs file at all.
When you rename the namespace, your routing starts pointing to the app.Controllers.FWController (not in app.Areas.Manage.Controllers.FWController), which resolved the conflict that was occurring before because you specified this in your Global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional },
new string[] { "app.Controllers" }
);
that request also neglects the MapRoute that contains .admin namespace. That could possibly be the reason i believe. Hope it helped you.
How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this:
http://site/catalogue/BrowseByStyleLevel/1
The 1 is Id of the study level (Higher in this case) to browse, but I'l like to reformat the URL in the same way StackOverflow does it.
For example, these two URLs will take you to the same place:
https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages
https://stackoverflow.com/questions/119323/
EDIT: The friendly part of the url is referred to as a slug.
There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter:
routes.MapRoute( "Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new { controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""} // Parameter defaults )
Now you can type whatever you want to at the end of your URI and the application will ignore it.
When you render the links, you need to add the "friendly" text:
<%= Html.ActionLink("Link text", "ActionName", "ControllerName",
new { id = 1234, ignoreThisBit="friendly-text-here" });
This is how I have implemented the slug URL on my application.
Note: The default Maproute should not be changed and also the routes are processed in the order in which they're added to the route list.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute("Place", "{controller}/{action}/{id}/{slug}", new { controller = "Place", action = "Details", id = UrlParameter.Optional,slug="" });
you have a route on the global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = ""}
// Parameter defaults )
you can define your own route like :
controller is the cs class inside the the controllers folder.
you can define your id - with the name you choose.
the system will pass the value to your actionResult method.
you can read more about this step here : http://www.asp.net/learn/mvc/tutorial-05-cs.aspx