My goal is to move away from an ISAPI filter that was set up and instead do all the rewriting/routing at the application level.
I have URLs like: product.aspx/2008C20080929.htm
I can correctly route those pages using the following RouteTable.Routes.MapPageRoute:
RouteTable.Routes.MapPageRoute("testRoute", "product.aspx/{page}", "~/routeTest.aspx");
However, some of the other URLS are "encoded", example:
product.aspx/%255COH%255C2008%255C20080929.htm
I am unable to route these pages with the previously stated route. Is this not possible? I'm getting 400 bad requests.
Instead of using URL Routing, I used IIS7's Rewrite Module
URL Rewrite
Related
Setup
ASP.NET-Core web application hosted at https://first-domain.com/
Using a load-balancer to place the site under https://second-domain.com/some/path such that a request to https://second-domain.com/some/path/Page1 passes the request to https://first-domain.com/Page1
Sending headers when requests are forwarded:
X-Original-Host = 'second-domain.com'
X-Original-BasePath = 'some/path'
X-Original-Url = 'https://second-domain.com/some/path/Page1'
Pages use the ASP.NET root path character (~) to refer to resources relative to the application root.
Using ASP.NET-Core middleware to dynamically route the requests based on the headers.
Problem
My middleware correctly routes the request to the page. Based on the headers, requests to https://second-domain.com/some/path/PageX correctly retrieve the resource at https://first-domain.com/PageX.
However, PageX's URLs which use the ASP.NET root path character (~) are resolving to / so then the client tries to access resources at https://second-domain.com/ which don't exist.
For example, if PageX.cshtml had a <img src="~/myImage.png> tag, the client's browser will try to retrieve resource https://second-domain.com/myImage.png instead of https://second-domain.com/some/path/myImage.png
Question
Is there a way to manipulate the request and/or response with ASP.NET-Core middleware such that the ASP.NET root path (~) is resolved dynamically?
In other words, I'm trying to set a virtual path dynamically without using infrastructure-defined virtual paths via IIS/Azure.
This can be accomplished by setting context.Request.PathBase from the middleware.
I'm building an Asp/.Net Web Application and I don't know how to create a URL Routing for make /blog/title-of-the-post redirect to post.aspx?id=2.
I already have on my database the column URL (and I want to use that).
Everywhere on my code for redirect to my post I already use the URL column (usign <%#Eval("URL") %>) but of course, does not work cause I do not have a Routing.
The site I'm building needs to have a users username as part of the url like:
mydomain/user1
I have set this up in global.asa as:
routes.MapPageRoute("SubscriptionList","{Username}/","~/subscriptionlist.aspx");
However if there are links on the site to pages that arent routed, for example, mydomain/login.aspx these get routed to the 'SubscriptionList' page. I presume that this is because the required URL '{Username}/' can actually be nothing. How do I set routing up so that the above only gets triggered when the Username actually exists?
The alternative, which I'm sure isnt best practice, is to set up a route for each page on the site.
I think this has to do with the order of your routes in your routing table.
From what I understand, you should make sure that this route is below others that have explicit routing set.
For instance, this should be below the route than handles the routing to Login.aspx as I think that rules are evaluated from top to bottom.
Or do I not understand the question=
I have an ASP.NET 4 HttpModule (see code below). When the url path starts with "/1.0" I want Cassini/IIS to go to MyService.svc. However, I don't want to show "MyService.svc" to the user (i.e. no update to the url in the browser). I want the user to see "www.something.com/1.0".
I was pretty sure that RewriteUrl isn't supposed to change the browser url, but in my case it does. Any idea why?
public void Init(HttpApplication context)
{
context.BeginRequest +=
delegate
{
HttpContext ctx = HttpContext.Current;
const string BasePath = "~/1.0";
if (path.StartsWith(BasePath, StringComparison.OrdinalIgnoreCase))
{
ctx.RewritePath("~/MyService.svc", "this/is/a/path", string.Empty, false);
}
};
}
P.S. I cannot use ASP.NET Routing because of the period/dot in the Url (see ASP.NET MVC Route IDs with a period).
Looks like you have the same problem as described here:
ASP.NET RewritePath not working as expected / URL in browser changing
Add the trailing slash to the url:
ctx.RewritePath("~/MyService.svc/", "this/is/a/path", string.Empty, false);
Also, I'm not sure if WCF engine would preserve PathInfo for you. Possibly you'll have to pass parameters with the URL as QueryString.
You need url routing of ASP.NET, and it's available since .NET 3.5 SP1.
For your case, I think it's easier to "route" instead of rewriting, and it's simpler to use.
Why? MSDN said this:
In ASP.NET routing, you define URL patterns that contain placeholders
for values that are used when you handle URL requests. At run time,
the pieces of the URL that follow the application name are parsed into
discrete values, based on a URL pattern that you have defined. For
example, in the request for
http://server/application/Products/show/beverages, the routing parser
can pass the values Products, show, and beverages to a handler for the
request. In contrast, in a request that is not managed by URL routing,
the /Products/show/beverages fragment would be interpreted as the path
of a file in the application.
You can also use the URL patterns to programmatically create URLs that
correspond to the routes. This enables you to centralize the logic for
creating hyperlinks in your ASP.NET application.
ASP.NET Routing versus URL Rewriting
ASP.NET routing differs from other URL rewriting schemes. URL
rewriting processes incoming requests by actually changing the URL
before it sends the request to the Web page. For example, an
application that uses URL rewriting might change a URL from
/Products/Widgets/ to /Products.aspx?id=4. Also, URL rewriting
typically does not have an API for creating URLs that are based on
your patterns. In URL rewriting, if you change a URL pattern, you must
manually update all hyperlinks that contain the original URL.
With ASP.NET routing, the URL is not changed when an incoming request
is handled, because routing can extract values from the URL. When you
have to create a URL, you pass parameter values into a method that
generates the URL for you. To change the URL pattern, you change it in
one location, and all the links that you create in the application
that are based on that pattern will automatically use the new pattern.
See ASP.NET Routing in MSDN Library.
I have a page, called foo.aspx and i d like to rewrite the url as bar.something
How to do this? How does url rewrite happens in asp.net
Should i create a generic handler?
or should i get some url rewrite modules and add to app?
This is done by configuring IIS, and will require IIS7, look here for configuration help:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
Remember, you also have the new Routing option with ASP.NET 4.0:
https://stackoverflow.com/questions/90112/iis-url-rewriting-vs-url-routing
You should also check this SO response:
IIS URL Rewriting vs URL Routing
Some basic info on the differences between URL Re-Writing and Routing:
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/