How to use URL Routing with database - c#

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.

Related

MVC HttpGet and HttpPost

Recently I have attended a training in mvc. The trainer said that - As per the security concerns we have to use HttpPost instead of HttpGet. Always use HttpPost.
Can anyone explain - what is the security issue when we use HttpGet?
When transmitting data over secure connection (https) body of the post request is encrypted and practically undreadable, you can only see address where data is going but not the data itself. Get on the other hand has no body and data has to be transmitted in either query string or as a path parameter. While it is true that query string does get encrypted as well, due to request logging on the server and browser it is possible to get hold of that data.
Anyone can insert image on public forum or stackoverflow with link to your web-site. Then happens next:
Browser looks at url in image tag
Browser find cookies corresponding to domain in url
Browser sends request to url with cookies of user
Your server performs action
Browser tries to parse response as image and fails
Browser renders error instead of image
But if you mark your action as Http Post only then this scenario isn't applicable for 90% of sites. But you should also consider that if hacker can create a form on other web-site then he still can make browser to perform request. So you need CSRF. Well, browsers made a lot to prevent cross-site requests, but it's still possible in some scenarios.

Redirect links with a fragment (#) - asp.net

I have a website that contains links in different places, each link takes the user to a specific document.
Those links are hard-coded, which makes every update to the site very difficult.
Recently the documents were updated and we would like to update each old link with the corresponding new one.
For example
(http://www.domain.com/OnlineDoc/default.aspx#ItemID=3551)
needs to be updated to
(http://domain.docfactory.com/#!doc/Toolkits/Item1-Administration-Doc)
Each item document is determined using the ItemID.
What I'm trying to do is programmatically redirect each old link to it's corresponding new one.
I've tried to use the URL Rewrite module but it seems like it's ignoring everything after the "#", that is important because it determines which document we need to direct to.
My questions are:
Would the URL Rewrite module work? (having a fragment with "#")
If not, would the Http module work?
I'm trying to find a way to easily update the links, without going to each web app HTML and do it manually.
The fragment isn't sent to the server. It's accessible only to the browser. So you need a solution which involves the client-side.
But you can use the URL Rewrite Module in combination with client-side scripting in OnlineDoc/default.aspx that puts the fragment in the path or query part of the URL using a temporary client-side redirect. This makes the document item ID visible to the URL Rewrite Module, which can perform a permanent server-side redirect to the correct URL.
To clarify:
User requests /OnlineDoc/default.aspx#ItemID=123
Client-side scripting on this page issues a "temporary client-side redirect" (in actuality, just an automatic navigation) to /OnlineDoc/default.aspx?ItemID=123
User automatically requests /OnlineDoc/default.aspx?ItemID=123
URL Rewrite Module intercepts requests, and responds with a permanent redirect to docfactory.com/#!doc/Toolkits/Item1-Administration-Doc
User automatically requests docfactory.com/#!doc/Toolkits/Item1-Administration-Doc
The first redirect is temporary because you cannot make permanent redirects with client-side scripting. The second redirect is permanent because the new URL should always replace the old one.
The overall experience for users using this method could be a bit unexpected though, since users going to OnlineDoc will get redirected up to twice before they reach the intended document. Most users probably won't notice the second redirect, but the first will likely be noticable, if only slightly.

Routing with a non optional parameter (not MVC)

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=

Why RewritePath changes the Browser Url?

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.

URL Routing Encoded URLs in web application

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

Categories