Request.Querystring after URL rewrite - c#

Is there an easy way to get the value of a Querystring after it has been rewritten to a nice looking url? (URL REWRITE)
For example:
www.example.com/Home?CatId=Shoes
Here I can use request.querystring which I can use to get data from the database.
www.example.com/shoes
But here I can't do that...

You can install url re-writer module on IIS 7.0 and then do the following within web.config to create friendly urls:
<rewriteMaps>
<rewriteMap name="CatsMaps">
<add key="www.example.com/Home?CatId=Shoes" value="/shoes" />
</rewriteMap>
</rewriteMaps>
And if you have all URL's stored in database then look into DBProvider:
http://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module

You could enumerate the rewrite map(s) and check each value but you could get more than one match obviously.

Related

How to exclude a URL from being rewritten by .Net Intelligentia Rewriter

I have a Webforms ASP.Net app that already has multiple rules defined for url rewriting and they are working. the app uses Intelligencia URl Rewriter, which unfortunately has no online/offline documentation whatsoever.
Due to one of these rules, a request for any aspx file is getting redirected to default.aspx, which is fine for all cases except when the aspx is Telerik.Web.UI.DialogHandler.aspx.
I tried to use the following configuration in rewriter config to NOT rewrite the url if it contains Telerik.Web.UI.DialogHandler.aspx, but it's not working.
Question
How would I exclude the url containing Telerik.Web.UI.DialogHandler.aspx from beinng handled by the Url Rewriter?
Config below is for excluding a url but it does not work
<rewrite url="^(.+)?Telerik.Web.UI.DialogHandler\.aspx(\?(.+))?$" to="$1"
processing="stop" />
An existing rule is causing rewriting when url conatins Telerik.Web.UI.DialogHandler.aspx and this rule is as below.
<if url="^((?!\.ashx|\.asmx|\.axd|\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js|install|error|auth\.aspx|default\.aspx|popup\.aspx|advanced\.aspx|digest\.aspx|ftb.imagegallery\.aspx|ckfinder).)*$">
<rewrite url="^~/(.+)?\?(.+)?$" to="~/Default.aspx?g=$1&$2" processing="stop" />
<rewrite url="^~/(.+)?\.aspx$" to="~/Default.aspx?g=$1" processing="stop" />
<rewrite url="^~/(.+)?$" to="~/Default.aspx?g=$1" processing="stop" />
</if>
I finally found the answer.
To exclude a URL containing the string Telerik.Web.UI.DialogHandler.aspx, the following rewrite rule must be placed as the first rule in the rewrite URL config section.
<rewrite url="^(/.*(Telerik\.Web\.UI\.DialogHandler\.aspx)(\?.*)?)$"
to="$1" processing="stop" />
It's important to place it as the first rule so that the chance of any existing rules applying to this URL is completely eliminated. The $1 used for to attribute makes sure that original URL is used as the value of to attribute.

what would happen when re-write URL is used in web config

If i had the below in the web.config file, and in the code behind I was using Request.QueryString("category") would this still return the string or just return and empty string.
<rewriter>
<rewrite url="~/products/books.aspx?category=books" to="~/products.books.aspx" />
</rewriter>
If you change the URL in the web.config file the the query string can not be used. so adding the value into a Session["id"] as an example will allow you to pass the values through, where ihave hav id u can change to a more meaninfful name.

EPiServer, id as key in a querystring, id hijacked by url-rewriter, use rewriter for specified location?

I have rewritten an old application that then have quite a lot of external applications that redirects to a specific url containging the ?id={someid} querystring.. however.. episerver seems to do something with the id-key.. since that's never displayed in Request.QueryStrings.AllKeys..So my guess is that epi is doing some kind of url-rewriting or something.. is there any way to get around this and be able to use the id-key for only a specific page/location?
I would suggest using the IIS 7 url rewrite module to transform the id querystring key to something that will not conflict with EPiServer, as id is used for the page id in EPiServers internal url after the friendly url has been converted within the url rewrite provider.
The IIS url rewrite module would give you a chance to change this before hitting EPiServer.
You could write a regular expression to capture the id param, so you could add the following rule to your rewrite configuration
<rule name="QueryString">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="(.*)id=([a-zA-Z0-9_-]+)(.*)" />
</conditions>
<action type="Rewrite" url="{R:1}?{C:1}oldid={C:2}{C:3}" appendQueryString="false" />
</rule>
Which would rewrite the url to /my-url-path/?oldid=123&another=param
More on how to implement as a rule in the IIS url rewrite module here
The querystring parameter id is reserved by EPiServer. You need to check it before EPi touches it.
Update: If you step up to EPi 7.5 the query parameter “ID” is no longer handled by EPi unless you register a EPiServer.Web.Routing.ClassicLinkRoute.

Remove Traiing Foward Slash using UrlRewritingNet

i'm doing some work on a legacy asp.net and umbraco site. It's using UrlRewritingNet to set up rules for url manipulation.
I'd like to create a rule in the UrlRewritingNet file to remove the trailing slash from the url.
eg. http://www.test.com/index.aspx/ to http://www.test.com/index.aspx
Can you please help.
<add name="noendslash"
virtualUrl="^(.*)/$"
rewriteUrlParameter="IncludeQueryStringForRewrite"
redirect="Application"
destinationUrl="~$1"
ignoreCase="true" />
In the later versions of IIS in the url rewrite section you can set up these SEO rules without having to change code. Just 'add rules' in the 'url rewrite' section of the website:
You can use the TrimEnd() method.
return inputString.TrimEnd('/'); // .NET 2 or newer
or
return inputString.TrimEnd(new char[] { '/' }); // legacy

Mvc 3 - Controller as image handler, how to pass a path?

I am trying to use a controller as an image handler, but how do i pass in a path to it?
Right now it looks like this (works for images without a path):
public void GetImage(string parameter)
{
var imageHandler = UnityGlobalContainer.Container.Resolve<IImageHandler>();
imageHandler.ProcessRequest(parameter);
}
But if i try to send in the path folder1\folder2\folder3\picture.jpg then it fails.
#Html.ActionLink("Show", "GetImage", "Utility", new { parameter = #"folder1\folder2\folder3\picture.jpg" }, new { })
produces this:
http://localhost:58359/Utility/GetImage/folder1%5Cfolder2%5Cfolder3%5Cpicture.jpg
and that leads to:
HTTP Error 400 - Bad Request.
How can i pass in a path to the controller using the normal mvc approach?
(I am using backward slashes and not forward slashes)
I have also tested using HttpUtility.UrlEncode on the parameter.
According to your code: The produced link in the html page should be:
http://localhost:58359/Utility/GetImage?parameter=folder1%5Cfolder2%5Cfolder3%5Cpicture.jpg
and the parameter variable should be correctly set to "folder1\folder2\folder3\picture.jpg" in the action method.
Notice that you might be vulnerable to directory traversal here.
In .NET 4.0 beta 2, the CLR team has offered a workaround.
Add this to your web.config file:
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
</schemeSettings>
</uri>
This causes the Uri class to behave according to the RFC describing URIs, allowing for slashes to be escaped in the path without being unescaped. The CLR team reports they deviate from the spec for security reasons, and setting this in your .config file basically makes you take ownership of the additional security considerations involved in not unescaping the slashes.
Can you not just decode the parameter?
http://msdn.microsoft.com/en-us/library/6196h3wt.aspx
Instead of calling the filename parameter 'parameter', and defining it in your route, call it 'filename' and DON'T define it in your route.
Your action code will be the same, but the filename will stop being part of the route and just be an ordinary URL parameter.
If you're afflicted by this season's fashion for disliking URL parameters, then you might find this repugnant, but that's just fashion and can safely be ignored.
Personally, I wouldn't pass paths like this into a web app, because I would be absolutely paranoid about creating traversal threats by mistake - I only ever pass in path-free filenames, validate them against a list and then fetch the file.

Categories

Resources