We need to add a .html file which should be part of the root directory of our mvc app so i added the following
Route AnnouncementRoute = routes.MapPageRoute
("Announcement", "Announcement", "~/Announcement.html");
Route DefaultRoute = routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The above code makes all the default actionlinks to add Announcement
http://localhost/Announcement?action=actionname&controller=controllername
How to make sure my actionlinks generated does not contain Announcement in the link as follows which is correct?
http://localhost/?action=actionname&controller=controllername
You can use RouteCollection.IgnoreRoute(). Here's a good usage example that does exactly what you're trying to do.
Related
I would like to create a member area on my site with the following URL patterns:
Pattern for logged out user:
domain.com
domain.com/About
domain.com/Blog
domain.com/Blog/1 (where 1 is the post ID)
But I also have a member area where I prefix the Url with Member like this:
domain.com/Member/MyProfile
domain.com/Member/MySettings
This seems simple, but I can't see an obvious way to make routing rules for this. I have:
routes.MapRoute(
"Member", // Route name
"Member/{controller}/{action}/{id}", // URL with parameters
new { controller = "Task", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This works great for the member when you are logged in, but unfortunately the first rule also matches the logged out view and Url.Action("Blog", "Home") produces a Url that looks like this:
domain.com/Member/Home/Blog
How do I tell Url.Action that it must form Urls with the default rule outside the member area?
You could use a real MVC area instead of trying to simulate one. There's also a video you might checkout. The idea is that you leave your default route definition in Global.asax and then add a Member area to your site which will have a separate route configuration.
Assume there is a link as follows:
domain.com/Link/Details/60/AbiWord-2.6.5-is-a-free-software-word-processor--Wind%2cMac%2cLinux%2c..
Since there are two dots and the end, mvc is confused by the extension i think so .
Therefore, I just face with "Server Error in '/' Application.".
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"ViewLink", // Route name
"{controller}/{action}/{id}/{title}", // URL with parameters
new { controller = "Home", action = "Link", id = UrlParameter.Optional, title = UrlParameter.Optional } // Parameter defaults
);
When i remove tow last dots at end then my page will be loaded. so how can handle this issue ? I dont want to remove those dots
So the problem here is that the request doesn't even make it to ASP.NET MVC. The webserver sees it as a problem before it gets there. :(
Apart from escaping the dots, or removing the trailing one, not sure what you can do.
It's my first MVC project and my customer doesn't want any links like xxx.com/Home/Index or something like that. When i change my controller name the browser looking for /Home and it gives me an ex. Bec there's no HomeController.
How can i change it the default controller "Home" with another one.
Just change your Default route:
Default route created in Global.asax (Unchanged)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Modify the defaults for the parameters (controller, action, etc.). Something like this:
Modified Default route (Changed)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "YourNewController", action = "SomeOtherAction", id = UrlParameter.Optional } // Parameter defaults
);
The routes are set in the Global.asax file in the RegisterRoutes(RoteCollection routes) routine. The default route is specified as:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You would need to change the "Home" part to your new controller name.
You can also set up additional routes as required (see here).
I have added a controller to my project named UserManager (automatically generated from the ado.net framework)
When I start the application, attempts to navigate to http://server/UserManager/ are met with a 404 error, but if I go to http://server/UserManager/Index the action is found and executes properly.
Is this a case of the controller not being called or is it just not treating index as the default action. Where are these properties set?
UPDATE
It seems that the problem derived from the fact that the default route is set to
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Permits", action = "ListApplications", id = UrlParameter.Optional }
This conflicts with the naming scheme for Usermanager (where the default is Index)
I struggled with ohow to add alternate routes that provided for default actions, but eventually figured out that the order of route addition determines which route takes the request (the earlier the route is added, the more chances it has to meet the criteria.)
Thanks
You need to ensure that the default route mapping specifies "Index" as the default action in your global.asax file.
Check that you have the following setting in your global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
IN REPLY TO YOUR COMMENT:
Only by way of adding new route mappings. You also need to be aware that the first matching route mapping will be applied, so the order you specify the mappings in Global.asax is crucial.
For example, we wanted our FAQ controller to work with a URL http://domain/faq/{id} without the action specified in the URL, so we declared the following mapping before the default:
routes.MapRoute("Faq", "Faq/{id}", new { controller = "Faq", action = "Answer" });
In Global.asax.cs, check the default route is set up:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional}
);
Also, check that the controller is called UserManagerController, and derives from Controller
In my ASP.NET MVC3 project, I have a folder called Content (the default folder for an MVC project). But I also have a controller called Content. And when I want to use the default actions of this controller, I simply use http://domain/content/, which is equivalent to http://domain/content/index. But IIS returns 403 error and thinks that I'm gonna get the directory list of the Content Folder. Well, this question is already discussed in this question. But I don't know how to rewrite my URL to append the default action to it. May someone help please.
You can get around this by changing routing configuration to specify:
routes.RouteExistingFiles = true;
You will then need to set up some ignore rules to prevent genuine static content being gobbled up by the routing engine.
For example, I have a folder called Touch in my app, and I also have a specific route for Touch. So the working config is:
routes.RouteExistingFiles = true;
routes.IgnoreRoute("Touch/Client/{*touchclientversion}", new { touchclientversion = #"(\d*)(/*)" });
I agree that this kind of thing should generally be avoided, but sometimes it's nice to have pretty URLs :-)
You can add a default route like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
In your case:
routes.MapRoute(
"DefaultContent", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Content", action = "Index", id = "" } // Parameter defaults
);