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.
Related
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
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.
I am using ASP.NET MVC, In localhost, using the web, I changed the start URL option it works fine there, once I published it to the main domain, it is not working on that. I hope anyone can help me. I want to redirect from
domain.com/
to
domain.com/Home/Index
Thanks a lot!
you need to change your default route as this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
on startup the default route is initialized and hence redirected to the above website as
domain.com/Home/Index
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" }
);
}
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.