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=
Related
I have a Web API that allows users to access/manipulate resources that "belong" to other users. It contains many routes similar to this:
/users/{userId}/resource
I'd like a corresponding set of routes that do the same on behalf of the currently authenticated user:
/resource
Ideally I'd like to get the second set of routes working without doubling the number of actions in my controllers, but I'm struggling to find the right hook. Ideally, I'd like to add a MessageHandler (or something) that intercepts each request, checks if it matches a route, and if it doesn't, prepends "users/" + ID of the authenticated user to the route and checks again. What is the best way to accomplish this?
One constraint: I'm using attribute routing to implement the first set of routes and would ideally like to pull this off without sacraficing that.
The hook is the action selector, as you can see here: Routing and Action Selection in ASP.NET Web API.
You can implement your own action selector to achieve what you want. Here is a sample on how to do that: Magical Web API action selector. From the same page:
We will create a class that implements IActionSelector, as that would allow us to plug into the hook provided by the Web API under GlobalConfiguration.Configuration.Services.
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.
how would i go about implementing something like the following...
example.com site has these pages:
example.com/id1/page1/
example.com/id1/page2/
example.com/id1/page3/
example.com/id2/page1/
example.com/id2/page2/
example.com/id2/page3/
and now i want to have that when i point domains example1.com and example2.com to example.com/id1/ page and example.com/id2/page1/ respectively.
the site is on azure and what i did was set a dns for all three domains (example.com, example1.com, example2.com) to point to the same ip.
and then on the home page of the site i do a redirect with
Response.Redirect(...);
but this means that the domains example1.com and example2.com are not seen in the browser url, but rather the urls such as example.com/id1/ and example.com/id2/ are seen instead.
what i would instead like is to have my site show as the actual domain in such a way that these URL's are never seen:
example.com/id1/
example.com/id1/page1/
example.com/id1/page2/
example.com/id1/page3/
example.com/id2/
example.com/id2/page1/
example.com/id2/page2/
example.com/id2/page3/
and instead they respectively show up as as
example1.com/
example1.com/profile/
example1.com/about/
example1.com/contact/
example2.com/
example2.com/profile/
example2.com/about/
example2.com/contact/
so what do i need to change to make the domains visible as such?
sorry for the long question :( hope you can help me.
You wouldn't do that using a redirect since, as you mentioned, you want to keep the URL displayed as the one typed in. What you are trying to do is URL routing. For ASP.NET, the process and configuration is explained here: http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
If your list of pages is limited, you can do routing statically. Or you can provide the route table based on a database of pages or something. The walkthrough here explains how to provide those routes: http://msdn.microsoft.com/en-us/library/dd329551(v=vs.100).aspx
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.
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