MVC hides virtual directories in IIS - asp.net-mvc

I have the default directory in my IIS server set as an MVC 4 Razor application. I have it set up so that dynamic actions can be passed into the url. For example.
www.mydomain.com/4
www.mydomain.com/5
Basically those two launch accounts for users with id's of 4 and 5 respectively.
Works great.
But, I have a virtual directory on the same server called admin. I would hope that I could navigate to www.mydomain.com/admin but sadly MVC intercepts it and thinks that i'm looking up the user id of amdin.
Is there a way to poke holes in the route to allow for virtual directories?
Thanks in advance!
/Eric
########## adding my route
//route for site root
routes.MapRoute(
"YourRouteName", // Route name
"{flightNumber}/{action}", // URL with parameters
new { controller = "Home", action = "Index", flightNumber = " " } // Parameter defaults
);

You could constrain your route:
routes.MapRoute(
"YourRouteName",
"{flightNumber}/{action}",
new { controller = "Home", action = "Index" },
new { flightNumber = #"\d+" }
);
Now it is required that you must always have a flightNumber which is one or more digits followed by an optional controller action.
Thus /5 will hit the Index action of Home controller and pass it flightNumber = 5 parameter and /5/admin would hit the Admin action of Home controller passing it flightNumber = 5 parameter.
Now if your ids are not integers but strings things will get complicated because the routing engine will no longer be able to disambiguate your routes. You could constrain the action part to a list of possible actions using a RegEx but find that approach a bit limiting because if someone adds a new action to the Home controller he would also need to update the route constraint.

Related

ASP.NET MVC4 Routing - Multiple routes to the same location

I am in the process of setting up a Single Page Application (SPA) and would like to setup, currently two routes. For instance:
Route 1: http://localhost - this is the default route which requires authentication (Admin area)
Route 2: http://localhost/<client>/<clients project name>/ - this does not require authentication (view only)
In the admin area, they setup the <client> and <clients project name>, therefore I know I need to setup this configuration in MVC4 Routes, but it is unclear to me how I would approach this.
Another caveat would be, if the <clients project name> was not entered into the URL, it would present a search page for that client.
One of the great things about routing in MVC is the ability to route anything to anywhere, regardless of whether the url matches the naming of controllers and action methods. The RouteConfig allows us to register specific routes to cater for this. Let me show you how you can achieve this.
Route 1:
This is handled by the default route in the route config.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional });
Hitting http://localhost will take you to the Home controller and the Index action method.
Route 2:
We can set up one route that will cater for http://localhost/<client> and http://localhost/<client>/<clients project name>
routes.MapRoute(
"Client",
"{client}/{title}",
new { controller = "Home",
action = "Client",
title = UrlParameter.Optional });
Hitting either http://localhost/bacon or http://localhost/bacon/smokey will take you to the Home controller and the Client action method. Notice the title is an optional parameter this is how we can get both urls to work with the same route.
For this to work on the controller end our action method Client would need to look like this.
public ActionResult Client(string client, string title = null)
{
if(title != null)
{
// Do something here.
}
}

How to route Dynamic Controller/Action Pattern in MVC 4 -Routing for ASP.net MVC 4

I hope it is a better way to write the route in MVC 4 and would like some feedback.
Could you let me know if the routing patterns below are the best way to do it in MVC 4?
I have an application that has a requirement to show the url in the following format.
http://domain.com/777-777-7777-777-7777/Page1
http://domain.com/777-777-7777-777-7777/Page2
The 777-777-7777-777-7777 is an account number. PageX is the report page number.
I am trying to avoid trashing other controller routes in my project.
Below is what I tried but need feedback if this will trash future controllers.
Report Number plus page route
routes.MapRoute(
"Report", // Route name
"{AccountNumber}/{ReportPage}",  
new { controller = "Report", action = "Navigate", AccountNumber = UrlParameter.Optional, ReportPage = UrlParameter.Optional});
Future Controller I don't want to trash.
routes.MapRoute(
"FutureController", // Route name
"FutureController/{action}/{id}",  
new { controller = "FutureController",id= UrlParameter.Optional});
Any feedback is appreciated.
I believe ur routes are OK. Its not gonna break with current setup. Only thing to remember when writing routes is that you put more specific routes first because they are processed in order and first matching route is picked by url engine. I must however say that your ReportPage parameter would contains values like page1 and page1 instead of numeric 1 or 2. I have seen someone writing rules where only the numeric portion would be received in action result. As far as I can remember it was something like
routes.MapRoute(
"Report", // Route name
"{AccountNumber}/page{ReportPage}",
new { controller = "Report", action = "Navigate", AccountNumber = UrlParameter.Optional, ReportPage = UrlParameter.Optional},new{ReportPage = "\d+"});
This way you will have urls like http://domain.com/account/page1 but in action methods you will receive the numeric values for ReportPage parameter. It would, however, not match when page number is absent. For this scenario you can add another route like the guy recommended in answer to this question

MVC Routing: Trying to get dynamic root value working

I am trying to define dynamic sections of my site with the root url of the site. I am having some trouble defining the right MVC Route for it. Can someone please help.
My desired url will look like this: http://website.com/[dynamic-string]
But I have other standard pages like: http://website.com/about or http://website.com/faq or even just http://website.com.
My routes don't work correctly with that dynamic string. As shown below.
This is the route for the dynamic-string.
routes.MapRoute(
"CommunityName", // Route name
"{communityName}", // URL with parameters
new { controller = "Community", action = "Community", communityName = UrlParameter.Optional }
);
This is the route for all other STANDARD PAGES
routes.MapRoute(
"Default", // Route name
"{action}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
My routes just don't match up. Everything either gets diverted to one or the other route depending on which route is declared first.
There is no difference between the two routes you mention. How can MVC know which url should be mapped to communityName and which to action? Any url can match both.
You can define your standard pages as a route (before the CommunityName route) or you can catch them in your Community action, see if the name matches a function in your Home controller and then call the right action function.
I've never done this before but you might be able to create a more intelligent routehandler that looks at your controller actions, checks if the action really exists and if true selects that route.
this is beacuse the routes are effectively the same. When you declare the action route you do not state any constraints to the route, for this reason anything will be assumed to be a the action name.
If you want two routes to capture at the same level then you must constrain the action names to those that exist on your controller, this way if it does not match it will pass to the next route.
You can see an example of and advanced constraint here:
http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

Use MVC routing to alias a controller

I have a controller called InstallationController, and a fancy report representation of an installation called a Rate Card, but the end user insists on calling installations themselves Rate Cards. I would like him to see the URL http://site/RateCard/Edit/3, where this gets routed actually as http://site/Installation/Edit/3. How can I do this in MVC 3 RC2?
A couple of options are, you can either rename the controller to RateCardController, or add a new route that directs to the Installation controller, like:
routes.MapRoute(
"RateCard", // Route name
"RateCard/{action}/{id}", // URL with parameters
new { controller = "Installation", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

ASP.Net MVC routing action name conflicts

We have a website that deals with artists and venues and we're developing it in ASP.net MVC.
We have our artist views in a folder (Views/Artists/..), an ArtistsController, ArtistsRepository and adhere to the REST action names such as Show, New, Delete etc.
When we first mocked up the site, everything worked well in our test environment as our test URLs were /artists/Show/1209
but we need to change this so the website appears as /artists/Madonna and /artists/Foo-Fighters etc
However, how can we distinguish between valid artist names and the names of the actions for that controller?! For example, artists/PostComment or artists/DeleteComment? I need to allow the routing to handle this. Our default Show route is:
routes.MapRoute(
"ArtistDefault",
"artists/{artistName}",
new { controller = "Artists", action = "Show", artistName = ""}
One way around this is for our website to visibly run on /artists, but have our controller renamed to the singular - ArtistController - as opposed to ArtistsController. That would go against the naming conventions we went with when we started (but hey!).
Do you have any other recommendations? If possible we could also route depending on the verbs (so PostComment would be a POST so we could perhaps route to that action), but I'm not sure if that is advisable let alone possible.
Thanks
The 4th parameter to MapRoute allows you to specify restrictions for values. You can add a route before this one that is for "artists/{action}/{id}" with a restriction on the valid values for action; failing to match one of your actions, it'll fall through to the next route which will match for artist name.
You would actually define multiple routes... the defined actions in your controller would go first with the default being at the bottom. I like to think of route definitions as a "big 'ole switch statement" where first rule satisfied wins..
routes.MapRoute(
"ArtistPostComment",
"artists/PostComment/{id}",
new { controller = "Artists", action = "PostComment", id = "" }
);
routes.MapRoute(
"ArtistDeleteComment",
"artists/DeleteComment/{id}",
new { controller = "Artists", action = "DeleteComment", id = "" }
);
routes.MapRoute(
"ArtistDefault",
"artists/{artistName}",
new { controller = "Artists", action = "Show", artistName = "" }
);

Resources