I have a problem with an asp.Net project. I have changed some things in my client. At first the register view was inside the account view by default but i wanted to change that because i did other changes in the code too. Now when i run it can't find the path(error 404) .I changed the view by clicking at my register controler => add view. I have read other posts about the mvc areas but i can't unterstant exactly what i need to change because i am a newbie. I tried that code. My controller name is register my method ActionResult is also register().
routes.MapRoute(
"Register", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Register", action = "Register", id = UrlParameter.Optional });
Your Register action method exists in the RegisterController now, not AccountController. So you need to update the calls to your old register action method (Account/Register) to the new one (Register/Register), in your layout / other views files
Change
#Html.ActionLink("Register", "Register", "Account")
to
#Html.ActionLink("Register", "Register", "Register")
This will generate Link with href value set to "Register/Register" which is a valid request for an existing route.
The bit of code you referenced is exactly the same as the default rule, except that if you enter a url with no path it will assume it should use a controller named Register using an action named Register. It will actually overwrite your default rule if it is placed first because all possible URLs match the URL pattern.
localhost:59436
localhost:59436/Register
localhost:59436/Register/Register
These all point to the same thing using your Routing rule. I'm not sure that is what you intended.
What is the name of the controller and action you are using to register? You need to update your links to use:
#Html.ActionLink("Register", "ActionName", "ControllerName")
Related
When I use this URL:
http://www.domain.com/Dashboard/Admin/Index
My page works as expected. Dashboard is an MVC Area, Admin is the controller and Index is the view.
If I remove the area name, like this:
http://www.domain.com/Admin/Index
Then I get the following error:
That's only kind-of expected; shouldn't it be a 404?
Why does MVC still successfully locate the controller and attempt to find the view when I don't specify the area?
How do I force it to return 404 when area is not included?
I've tried changing namespaces but it doesn't make a difference.
By default the built-in controller factory will find all implementations of matched controllers regardless of namespace. While the view engine will search for the associated view in context of the request (i.e. won't search in an area if the area route has not been matched). In short you need to limit the route engine definitions to only search in 'defined' namespaces, then the 404 will be thrown.
This is accomplished by using an overload of the MapRoute method, and passing the namespaces:
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
);
For experimental purposes to see what the controller factory does with multiple controllers of the same name, try adding another AdminController to the root Controllers folder and then run the same request.
My ASP.NET MVC site allows users to register and give themselves user names, which will be unique and allow others to browse their pages with a clean URL that includes their name, like Twitter, Facebook, LinkedIn etc. do.
For example:
mysite.com/michael.guthrie
mysite.com/john
mysite.com/john/images
mysite.com/john/blog
etc.
The problem is that the first URL segment might be used for other "regular" controllers/actions, like:
mysite.com/about
mysite.com/register
So basically I seek for a routing scheme that says something like: If the first URL segment is a known controller, treat it as a controller (and parse the relevant action and parameters as usual), but if not - treat it as a user name, and pass it to a dedicated controller+action which will parse it and continue accordingly.
I don't want a solution that will enforce me to add routes for every specific controller that I have, such that after the routing module will go over all of them and won't find a match, it will get to the last one which defines a route for this special user name segment. The reason is primarily maintenance (I must remember to add a route every time I code a new controller, for example.)
I assume I can implement my own MvcRouteHandler / IRouteHandler but I feel there must be simpler solution that won't have me tweak MVC's out-of-the-box routing mechanism.
Note: I've read How to achieve nice litle USER page url like facebook or twitter? and it doesn't answer my question, it's just says that there is a URL rewriting module.
Do you know any good, elegant, clean way to achieve that?
You should have your first route be your Usesr route, with a route constraint along the lines of what I described in this answer: MVC routing question.
If your route is in the form {username}/{controller}/{id}, this route should cover all contingencies.
in the global.asax file you can map your routes
in the registerRoutes() method you can do something like this:
routes.MapRoute(
"ToonStudenten", // Route name
"{controller}/{action}/{userID}, // URL with parameters
new { controller = "Docent", action = "ToonStudenten", userID = UrlParameter.Optional} // Parameter defaults
);
I believe you can change the way your views look with this mapRouting, not entirely sure how though.. will try and search it up
You may want to take a look at this post:
MVC 3 keeping short url
You don't need to set a route for each URL. With a little help from route constraints you can do something like this:
routes.MapRoute(
"Home", // Route name
"{action}", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults
new { action = "TaskA|TaskB|TaskC|etc" } //Route constraints
);
routes.MapRoute(
"Account", // Route name
"{action}", // URL with parameters
new { controller = "Account", action = "Logon" }, // Parameter defaults
new { action = "Logon|Logoff|Profile|FAQs|etc" } //Route constraints
);
I have an address "http://localhost:3579/MusicStore/StoreManager" which is really showing "http://localhost:3579/MusicStore/StoreManager/Index".
I want to go to another another address on the same level from the index: "http://localhost:3579/MusicStore/StoreManager/Edit". Edit is a view inside the StoreManager folder, so a 2nd level view.
I'm confused as to which controller I'd even put the method in. I tried putting my "public ActionResult Edit" in MusicStoreController, but it wasn't recognized. How can I do this?
It sounds like your action is in the right place, but you will need to make sure there is a route specified to route your URL to that action. Make sure a route like this is specified in your global.asax or area registration file if your project is using areas:
context.MapRoute(
"MusicStore_Edit",
"MusicStore/StoreManager/{action}",
new { action = "Index"}
);
I ve seen in twitter, i can get a user view page by just typing in the url say http://twitter.com/pandiyachendur. How to do the same with asp.net mvc? I dont know how twitter does it?
You need to be careful about the order in which you declare your routes. Since there is no common element to a /{username} URL, you need to declare it as the last 'catch-all' route, after all of your specific routes.
RouteTable.Routes.MapRoute(null, "LogIn", new { controller = "Account", action = "LogIn" });
RouteTable.Routes.MapRoute(null, "LogOut", new { controller = "Account", action = "LogOut" });
// ... other routes go here ...
// Final catch-all route to map /{username} to the Account.Details action.
RouteTable.Routes.MapRoute(null, "{id}", new { controller = "Account", action = "Details" });
It's also worth remembering that you need to extend your validation on usernames to prevent people from choosing names that conflict with the specific routes (e.g. LogIn).
I imagine that they have some regular exception that checks the request to see if it matches something that could be a user's profile and then push that request to an appropriate controller action.
They'd likely might list first all of the exceptions are static routes, like "/invitations", and then pass everything else to a default controller action that attempts to display a user's page.
I've created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.
example.com/Home/About
example.com/Home/ContactUs
example.com/Home/Features
You get the idea. I'd rather not have to create a controller for each one of these as the actions simply call the View with no model being passed in.
Is there a way to write a routing rule that will remove the controller from the URL? I'd like it to look like:
example.com/About
example.com/ContactUs
example.com/Features
If not, how is this situation normally handled? I imagine I'm not the first person to run in to this.
Here's what I've done previously, using a constraint to make sure the shortcuts don't conflict with other routing rules:
routes.MapRoute(
"HomeShortcuts",
"{action}",
new { controller = "Home", action = "Index" },
new { action = "Index|About|ContactUs|Features" }
);
Add defaults for the controller names in the new statement. You don't have to have {controller} in the url.