URL Rewriting Not working using Global.asax - c#

I am trying to do URL Rewriting from Global.ascx file but every time it redirects to Default.aspx page and ignores the Tab id as i am doing it in DotNetNuke ver. 06.01.03 (108). Here is the code:
if (CurrentURL_Path.Contains("scientific-cameras"))
{
HttpContext.Current.RewritePath("~/Default.aspx?TabId=105");
}
I have created a page abc.aspx
I want that if "scientific-cameras" is contains in the url then it redirect to the page (abc.aspx) and 105 is TabId for abc.aspx.

Use Routed Table URL Routing with ASP.NET 4.0
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0

More than likely this is a conflict with the DNN stuff. There is already a friendly URL structure in DNN. I would recommend using this rather than doing your own, as you don't want too many cooks in the kitchen.
You can specify custom URL's in "Host" -> "Host Settings" -> "Friendly URL Settings".

Related

To Modify the .htaccess for 301 Redirect with Asp.net on the fly (Dynamically)

I make changes using Cpanel / Redirect manager of a Site.
Based on the Changes made on Cpanel for URL redirection, i.e. Say Old Url -> Current Url, I want to modify the .htaccess file dynamically. How do I achieve this using Asp.net?
I assume you want to do URL redirection in ASP.NET. See more information about this topic. http://www.iis.net/downloads/microsoft/url-rewrite.

Redirecting url to index.aspx page using standard asp.net3.5 and web.config

I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.

URL rewriting and html canonical link in the header to avoid duplicate page issue for search engines

I am using Intelligenicai.URLRewriter.dll to rewrite my website URL it is working fine i followed the following link as an example
http://anupkumargupta.wordpress.com/2011/05/28/url-rewriting-with-urlrewriter-net-simplest-way-asp-net-c-sql-blog/
On the other article i read about URL rewrite can create problems for search engines as now we can access same page with two different URL one without URL rewrite and one with URL rewrite. (Check this link http://thecodebug.com/?p=296)
Above mentioned link talks about search engines can penalize if they find duplicate contents on the website. This article (http://thecodebug.com/?p=296) talks about "html canonical link in the header" if we don't add them then we can tell search engine that this url is canonical.
Now my question is this applicable to even if i use Intelligenicai.URLRewriter.dll and if this is the fact what are the precautions i should take to avoid such scenario.
Example of my URL
With URL Rewrite
http://www.xyz.com/Article/en-US/19/87/let-the-spirit-of-our-nation’s-founders-guide-us.aspx
URL without rewrite
http://www.xyz.com/Article/ArticleDetails.aspx?Language=en-US&PageID=19&ArticleID=87
Part of web.config
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/>
</rewriter>
You can add 2 rules: One rewriting the new-style links to the physical URLs then stop processing rules, and then follow it with a rule to rewrite direct requests to the aspx page to the new style URL, 301 permanent redirect, and stop processing rules. The 301 redirect is important! If you can't get it to do that with your rewriting DLL then you can redirect the URL to a special hidden page that will accept the old page as the querystring or looking at the referrer, then mapping it out to the new page using a 301 redirect there.
UPDATE: Here's some more information:
Using Intelligenica UrlRewriter, you would set up two rules, in this order:
The first rule uses the rule you have already set up for redirecting the fancy URL to the physical file.
The second rule takes the path to the physical file and either (a) 404 or (b) 301 permanent redirects to the fancy URL. If you have a single page serving up multiple pages of content, you may be better off just stopping processing with a 404 error. Also, as long as you never use the old-style links anywhere in your site, you should be okay. If at ANY TIME you used the old style links (before you rewrote the URLs), you definitely need to make sure that you have rewriting set up to redirect or 404 the page so the new style URLs are forced.
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/><!-- rewrites URL -->
<rewrite url="~/ArticleDetails.aspx?Language=(.+)&PageID=(.+)&ArticleID=(.+)" to="~/Article/$1/$2/$3.aspx" processing="stop" permanent="true" /><!-- Redirects old page to new url with 301 -->
</rewriter>
Hope this helps.

Getting text after URL in asp.net / URL Rewriting (sort of!)

My app is a very simple "one page" type app-
It has Default.aspx
I'm basically trying to get, for example:
www.myappurl.com/this is my text
I want to get hold of "this is my text" from the above example.
This will be displayed on the page (for now)
I didn't really want to have to use any complext url rewriting things for this...
(My hosting provider uses IIS6)
I tried using a 404 handler, but this is a bit long winded, and i'm using shared hosting, that can't set the "execute url" on custom 404 pages.
Any other ideas?
You can add a mapping for all requests with the * extension to the ASP.NET isapi dll (GET/POST) verbs. You will need to uncheck the "verify file is on disk" checkbox when mapping the extension in IIS. (In IIS7 integrated mode, you map the extension in the web.config as well). Note that this will caause everything to be served by asp.net, even images and script files, which can slow things down.
Then create a handler mapping in your web.config to a http handler you create.
From there, in the ProcessRequest() method of the handler, you have access to the HttpContext that spawned the request and can manipulate the URL from there.
That is the easiest option, you could also create a HttpModule, or have the default page at root redirect to http://www.domain.com/default.aspx/this is my text, in the code-behind of default.aspx, you will be able to get the text following the page and slash.

Using HttpModule with .html file extension

I need access to session in httpmodule. It works fine when my page is a aspx page, but context.session is null when the request url is .html
I have .html mapped to use aspnet_isapi.dll
I am trying to access session in context_PreRequestHandlerExecute and I have httpmodule inherit IReadOnlySessionState
From my experience IReadOnlySessionState and IRequiresSessionState only apply to HttpHandlers.
See the following SO links on how to implement it:
Can I access session state from an HTTPModule?
IIS HttpModule unable to set Session
It does not work with HTML extension because aspnet_isapi.dll does not handle the extension, but handels .aspx pages.
You should use another extension instead of using HTML and you should register the new extension in IIS Application Configuration (Web Site Properties -> Home Directory tab -> Configuration button -> Mappings tab). Use .aspx as an example to add your own extension.

Categories

Resources