Route url with aspx extension to mvc route - asp.net

I have a web site posting data to an old web app url mywebsite.com/Confirm.aspx
I am writing a new app using asp.net mvc and i would like that same url post to apply to an action called "Confirm" on my controller "Processor". Is it possible to do this using Routing in asp.net mvc? if so how?

There is a workaround.
You can add below request handlers in the web.config.
<system.webServer>
<handlers>
<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="AspxlFileHandler" path="*.aspx" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Then you can create route definitions:
routes.MapRoute(
name: "LegacyHtml",
url: "{page}.html",
defaults: new { controller = "Home", action = "LegacyPage", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "LegacyAspx",
url: "{page}.aspx",
defaults: new { controller = "Home", action = "LegacyPage", page = UrlParameter.Optional }
);
Next, you can either handle request in the controller action mentioned in routes.
Or you can further redirect the actions to other pages / sites based on value of Page path variable.
This is explained beautifully in this blog.

Related

How to handle routes with image extensions

I have action:
public ActionResult Thumbnail(string image)
{
return GetThumbnail(image);
}
I am trying to access it with the next request:
http://localhost:60955/thumbnail/imagename.png
In config I have:
<add name="Png" path="/thumbnail/*.png" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
And in routes configuration:
routes.MapRoute(
name: "Thumbnail",
url: "thumbnail/{*image}",
defaults: new { controller = "Image", action = "Thumbnail" }
);
So it works for the above image URL. But I need this to work for any subfolder and the following returns 404:
http://localhost:60955/thumbnail/screenshots/imagename.png
And when I add slash it works again:
http://localhost:60955/thumbnail/screenshots/imagename.png/
Can I make it work with no trailing slash?
I feel like I need to customize the handler path in the config but cannot figure out how.
You should use query string for this case
URL change to this
http://localhost:60955/thumbnail?image=screenshots/imagename.png

Change the URL root path in ASP.NET MVC or IIS

I'm working with a server with a custom URL: http://example.com/site/ and as you can see I need to change the default root path ("/") of my application to prevent 404 errors. Using IIS rewrite module with a outbound rule seems to work, with all html links and references being converted properly to query the internal website. The problem is when a Redirect() or RedirectToAction() method is used in my controllers, the internal name of the website is dropped so a 404 is caused. This is my IIS outbound rule:
<rewrite>
<outboundRules>
<rule name="Add path prefix to urls" stopProcessing="true">
<match filterByTags="A, Form, Img, Link, Script" pattern="^/(.*)" />
<action type="Rewrite" value="/site{R:0}" />
</rule>
</outboundRules>
</rewrite>
So to clarify: I have http://example.com/site/ when a redirection occurs to Account/Login, it becomes http://example.com/account/login instead of http://example.com/site/account/login . I guess the RouteConfig has to be tinkered with but I don't know how, or if I can do this in IIS. I have the following in my RouteConfig class:
routes.MapRoute(
name: "SiteRoot",
url: "site/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
Thank you.
Assuming the issue is only with pages protected by the authorize annotation, you probably need to update the /App_Start/Startup.Auth.cs file to change the login path. You're looking for LoginPath = new PathString("/Account/Login"), which you'll change to LoginPath = new PathString("/site/Account/Login"),. In the MVC 5 starter site I tested in this was on line #28.
In my testing with your redirect rule and route configuration normal RedirectToActions worked fine, but if you're having trouble you might try removing the "Default" route mapping so that URL structure isn't selected by the router when trying to redirect.

Routing for .js URL in ASP.Net MVC4

Near the bottom of my routing registration, if a URL has a .js extension, I'd like to handle it with a particular controller (most .js content is served statically, but some is special and needs to be served via a controller). However, the following route is being skipped, and the catch-all route is handling the request.
routes.MapRoute("ContentScript", "{script}.js",
new { controller = "Content", action = "Script" },
new[] { "NameSpace.Controllers" }
);
What is the right way to do this?
In the route directly after that one every remaining request is routed like so (and this works and catches the .js files), so the issue is not in any part but the url parameter):
routes.MapRoute("ContentScript", "{*path}",
new { controller = "Content", action = "Index" },
new[] { "NameSpace.Controllers" }
);
I also tried the following, without success:
routes.MapRoute("ContentScript", "{*script}",
new { controller = "Content", action = "Script" },
new { script = new RegexConstraint("\\.js$") },
new[] { "NameSpace.Controllers" }
);
you have to add handler to the web.config so it can handle it ...
some thing like that :
<system.webserver>
<handlers>
<add name="scripts" path="*.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webserver>
stackoverflow link #1
stackoverflow link #2
The problem was an IgnoreRoute that I was unaware of (the RouteRegistry.cs file is 1,469 lines long... I have not studied it in its entirety, yet). js files are being handled in managed code--they were just being taken out by this before my route could handle the request.
routes.IgnoreRoute("{*path}",
new { path = new RegexConstraint(#"[^?]*\.(gif|jpe?g|png|ico|js|swf|css|txt|html?|xml|pdf)") }
);

How can I redirect a subdomain to an 'area' in ASP.NET MVC?

We have the following three external visible 'endpoints'
api.domain.com
demo.domain.com
my.domain.com
We're using MVC5 and ideally we'd like to steer each of the above sub-domains into it's own area (per MVC terminology).
i.e.
api.domain.com => route into WebAPI (as a separate 'area'? Not sure)
demo.domain.com => route to 'demo' area
my.domain.com => route to 'my' area
support.domain.com => route to 'support' area (and so on)
However, I couldn't find a way to configure this. Does anyone know how we can setup the above?
The end goal is to have all the above as a single MVC site that we can deploy to Azure Cloud Services.
Did you look into Url Rewriting? if you are using IIS then you can download url rewrite Module and then in web.config you can do something like this:
<rewrite>
<rules>
<rule name="Rewrite sub-domain to dir" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^subdomain\.domain\.co\.uk$" />
</conditions>
<action type="Rewrite" url="subdomain/{R:1}" />
</rule>
</rules>
</rewrite>
Source: http://www.azurecurve.co.uk/2011/12/how-to-rewrite-a-sub-domain-to-a-directory-in-iis/
refer to the answer at Mvc area routing?
The answerer's example for an area under the /Admin/ folder is:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}

FormsAuthentication.GetRedirectUrl Always Returns the Default

I have an ASP.NET MVC app and am using Forms auth. When going to a page that requires authentication, meaning there is an [Authorize] attribute on the controller action, it redirects the user to the login page with a return url like http://localhost/Login?ReturnUrl=/MyAuthorizedUrl.
This is how my config is setup:
<authentication mode="Forms">
<forms loginUrl="~/Login" timeout="2880" defaultUrl="~/" />
</authentication>
This is how I'm getting the redirect url:
var url = FormsAuthentication.GetRedirectUrl( model.Email, model.RememberMe );
This always returns the default url.
What is causing this?
I assume you would like to get "MyAuthorizedUrl" as the result of FormsAuthentication.GetRedirectUrl?
You'll need to insert a hidden input field that mirrors ReturnUrl=/MyAuthorizedUrl, e.g. name="ReturnUrl" value="/MyAuthorizedUrl".
The reason is that the login page is requested via GET with the ReturnUrl, but the POST goes to /Login (without any parameters).
Alternatively change the form action attribute to include the ReturnUrl parameter.
If your login form:
#using (Html.BeginForm
(
"Login",
"Account",
new { ReturnUrl = Request.QueryString["ReturnUrl"] },
FormMethod.Post
))
Replace "Login" with your action name and "Account" with your controller name.

Resources