Considerations when turning on RouteExistingFiles - asp.net-mvc

I am looking to generate some CSS files dynamically in my Content folder.
At the moment, this folder has an ignore route (routes.IgnoreRoute("Content/{*wildcard}");) and, I'd like that to remain, as I don't need/want most of my content folders to go into the MVC request lifecycle.
Example:
routes.MapRoute(
"DynamicCSS",
"Content/Css/Dynamic/{guid}.css",
new { controller = "Site", action = "GenerateCSS" },
new { guid = #"^([0-9a-fA-F]){8}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){12}$" }
);
//If the file has already been generated, IIS should just return the file, saving a request in MVC.
routes.RouteExistingFiles = true; //was formerly false
//Ignore routes
routes.IgnoreRoute("Content/{*wildcard}");
I have a couple questions/concerns about this setup:
Will this work? Routes in ASP.NET MVC are lazy, but I don't know if the ignore routes are checked first. There's no documentation (I've Googled!) on this form of usage.
Are there any security implications to consider when switching on RouteExistingFiles? I don't want IIS to pick up any of my Model/Views folders by directly referencing them.
Many thanks for any suggestions.
Edit:
After further research, I have found an article on my first issue.

Scott Hanselman got a blog post "Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side" in which he talked about his too. http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
Hope this helps,
Ray.

Related

ASP.NET Core 2: how to RedirectToPage with area?

RedirectToPage("Companies") will redirect to /Pages/Companies.cshtml (from an ASP.NET MVC controller)
But what if want redirect to this page /Areas/MyArea/Pages/Companies.cshtml ?
All those and many others don't work:
RedirectToPage("/MyArea/Companies.cshtml")
RedirectToPage("MyArea/Companies.cshtml")
RedirectToPage("./MyArea/Companies.cshtml")
RedirectToPage("/MyArea/Companies")
RedirectToPage("MyArea/Companies")
RedirectToPage("./MyArea/Companies")
Sometimes I get "Page not found" error. Sometimes get "Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page". There are no Pages folder. I know all this can change all rules again.
P.S. Razor pages configred with plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing added.
Use the overload of RedirectToPage that takes an object representing RouteValues:
return RedirectToPage("/Companies", new { area = "MyArea" });
Note that the '/' is required if you use RedirectToPage in a controller (or anywhere outside of a Razor Page). Otherwise it is not required (but will still work).
This works for me:
return RedirectToPage("/Companies", new { area = "MyArea" });
Works under plain .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); no specific routing configured.
I feel this will be a popular question... Thanks to Mike Bring, he show me a path.
P.S. If you have Pages folder - all rules will be changed once more. That is the way how "Razor Pages" try to run out from "MVC magic"

Mvc 3 redirecting routes behaving weirdly

I have been trying to solve this problem for a couple of days now with no success.
The case is i have rewritten an old web forms site to mvc 3 and have a bunch of legacy url:s that need to be redirected to new url:s
This is what the 2 routes look like in global.asax that should catch all requests to old url:s
routes.MapRoute(
"legacyevents",
"events/{*slug}",
new { controller = "Redirect", action = "RedirectLegacyRoute" },
new[] { "Web.Controllers" }
);
routes.MapRoute(
"legacyarticles",
"articles/{*slug}",
new { controller = "Redirect", action = "RedirectLegacyRoute" },
new[] { "Web.Controllers" }
);
The weird thing is now that when a request looks like this
events/randomevent__12.aspx
everything works well but if the E in events is upper case Events/randomevent__12.aspx
asp.net somewhere adds another events word to the url so it looks like this when it hits the RedirectController
events/events/randomevent__12.aspx
Due to lack of knowledge about SEO when i wrote the web forms app few years back alot of incoming links to the old urls have mixed casing :( so i really have to fix this issue
The route that should handle articles works as intended and does not care about the casing of the incoming request url which which makes this case so weird, since the routes are pointed to the same RedirectController.
Any help will be appreciated and if you need any additional info i will happily provide it
best regards
//K
You do know that you can use full power of regular expressions in your route matching?
See examples here.

ASP.NET MVC - 301 Redirect - SEO issue

So I have an ActionName like so:
[ActionName("Chicago-Bears")]
public ActionResult ChicagoBears() {
return View();
}
Google has this indexed as: http://www.example.com/Teams/ChicagoBears
I'm stuck using IIS6 and have no access to IIS myself.
Of course now, it has a hyphen in it. So, Google will show a 404 if someone clicks on that link.
How do I setup a 301 redirect in this instance? I can't create another method called ChicagoBears(), so...
Thanks guys.
Create a route for Teams/ChicagoBears that points to an action that gives a permanent redirect.
In Global.asax...
routes.MapRoute("ChicagoBearsRedirect",
"Teams/ChicagoBears",
new { controller = "Teams", action = "RedirectChicagoBears" }
);
In TeamsController...
public ActionResult RedirectChicagoBears()
{
return RedirectToActionPermanent("Chicago-Bears");
}
The URL Re-Write Module is your friend. Learn it, live it, love it...
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
I used it extensively when I migrated from DasBlog to WordPress. All my old blog URLs are re-directed using 301's to the new ones. Highly recommended.
UPDATE: There are URL re-writers for IIS6. A quick google search turned up:
http://www.isapirewrite.com/
http://urlrewriter.net/
(Found via http://forums.iis.net/t/1160436.aspx.)
UPDATE: This blog I referenced seems to no longer be available so I updated the link to reference the internet archive version.
Check out this blog post for a great solution*: https://web.archive.org/web/20160528185929/http://www.eworldui.net/blog/post/2008/04/25/ASPNET-MVC-Legacy-Url-Routing.aspx
Essentially what he is doing is creating a reusable class that can be used for multiple routes, and they just issue a permanent redirect to the specified Action method.
**Note: This is not my blog, but one that I simply came across.*
Little late to the party on this one, but I wrote a blog post about permanent redirects for legacy routes that allows this -
routes.MapLegacyRoute(
null,
"Teams/ChicagoBears",
new { controller = "Teams", action = "ChicagoBears", area="" }
);
The Location to redirect to is generated using the route values using Url.Action, so as long as you have a route in the RouteTable that matches the route values, the 301 redirect will work as intended. In your example, the generated URL should be http://www.example.com/Teams/Chicago-Bears when the URL pattern matches "Teams/ChicagoBears".
I won't repeat the code here as there's quite a bit and it's on the blog

I'm getting a "Does not implement IController" error on images and robots.txt in MVC2

I'm getting a strange error on my webserver for seemingly every file but the .aspx files.
Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:
The controller for path '/robots.txt'
was not found or does not implement
IController.
I'm sure it's something to do with how I've setup routing but I'm not sure what exactly I need to do about it.
Also, this is a mixed MVC and WebForms site, if that makes a difference.
You can ignore robots.txt and all the aspx pages in your routing.
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*robotstxt}", new {robotstxt=#"(.*/)?robots.txt(/.*)?"});
You might want to ignore the favicon too.
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
You can adjust the regular expression to exclude paths.
Haacked from the source.
The ignore route given above didn't work for me but I found a similar one that did:
routes.IgnoreRoute("{*staticfile}", new { staticfile = #".*\.(css|js|gif|jpg)(/.*)?" });
This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to. E.g.,
#Html.Action("Main", "Navigation", new { area = string.Empty })
I found another solution too... While I don't think I'll use it, it's worth showing here in the answers:
The following should (in theory) ignore looking for controllers for anything with a '.' in it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter contraints.
);
Do you still have:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
... in your Global.asax.cs?
MVC puts it there by default, and it's supposed to handle this.
If you do, then the problem may be how you're mixing MVC and WebForms.
I encountered this error when I request resources that did not exist.
Specifically, I was requesting a custom IE css file:
<!--[if lt IE 8]>#Styles.Render("~/Content/ie7.css")<![endif]-->
(These are condition comments, interpreted by IE)
However, the actual resource existed on ~/Content/ie/ie7.css.
So, without any modifications to the routing, the error was solved by using the correct url of the resource.

How to do routing for Phil Haacks area prototype for ASP.NET MVC 1.0?

I am building a simple web site for a client and this is my first time with ASP.Net Mvc.
For the production i need to use MVC 1.0 and the most efficient way to saparate Admin logic from the rest of this site is using Areas.
The fact that i couldnt use MVC 2, so i have used the Haacks area prototype and everything was ok.
I want to write a custom routing for paging results but i couldnt get it done.
routes.MapAreas("{controller}/{action}/{id}",
"Adore.Web",
new[] { "Admin" });
//my custom routing
routes.MapRoute(
"PagingServices",
"Admin/Services/{pageNumber}",
new { area = "Admin", controller = "Services", action = "Index" });
routes.MapRootArea("{controller}/{action}/{id}",
"Adore.Web",
new { controller = "Home", action = "Index", id = "" });
As you see above i am trying to get this "Admin/Services/1" but couldnt figure it out.
How can i do it, thanks in advance !
Have you tried Phil Haack's Routing Debugger? :) http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
That might help.
Also, it looks like the problem might be that your first route is going to match anything starting with /Admin. Try moving your custom route to the top since routes are evaluated in order.

Resources