Search

Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts

May 25, 2009

Issues with Web.config in IIS 7 and Modules (in Vista)

A simple web application runs fine on internal web server, but the time we put it on IIS7 specially in Windows Vista, its start giving configuration error, first and foremost is issue with Modules.

This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".

And its points to <handlers> section under <system.webServer> section. The issue related to IIS don’t have ASP.NET installed. You can check your Windows feature, although you have installed IIS7 and compatibility for IIS6, ASP.NET is not getting installed automatically. Here is the Windows feature should look like as in following image.

No_ASP.Net

You can find this window from Control Panel –> Programs and Features –> Turn Windows features on or off [Left panel]

TurnWindowsFeaturesOnOrOff

If you have ASP.NET installed on your IIS 7.0 then you have to change the configuration from applicationHost.config file, which resides in %windir%\system32\inetsrv\config\applicationHost.config. You can find the entry for handlers and it has value Deny for property overrideModeDefault, change it to Allow.

<section name="handlers" overrideModeDefault="Deny" /> to <section name="handlers" overrideModeDefault="Allow" />

While saving file I am pretty much sure that it asks for Administrator account although your role is Administrators as you are not owner of that file you can’t make change to file, so solution for this is login as Administrator and do the changes. For vista Administrator is not active for login so find my post which help you to login as Administrator.

Sep 9, 2008

Extended URL rewriting

I was having one requirement for my .net application. In which I want to parse the following url. Handling request with out extension.

http://mysite.com/imran/
http://mysite.com/armaan/

For these url we can't user ASP.NET URL Re-Writing, as there is no aspx extension added to this url.

When you found such request and its not exists then IIS will give 404 error, but we need to parse such url and do some validation and give them proper output. In this case lets say I would like to see the profile of user imran, then I make request to http://mysite.com/imran/ which gives me the profile of user imran.

This is the normal requirement, we can do it easily in php and java. But in .net this is bit tricky to get such functionality working.

If we use http handler, then also we are not able to identify the request as it has no extension. As its is not reaching to the application, IIS itself reject this request, no global.ascx or web.config

I found very easy solution for this solution. This requires some setting on virtual directory and code in our system to handle such request.

As I mention eariler when you send request to http://mysite.com/imran/ which not exist; IIS will raise 404 error, what we do is, we trap this error and get the requested page or url and do some work and give the result.

IIS Setting
In IIS we have to change default 404 error handling to aspx page, bye default 404 page is C:\WINDOWS\help\iisHelp\common\404b.htm with Message Type as File. See the image



We need to change this to forward all such request to our application, once we got control in aspx page then we can do anything on such request.

For this we need to change Message Type, set it to URL and the URL will be /MySite/Handle404.aspx. See the image



Code
We have to write few lines of code in Handle404.aspx to handel this request.

protected override void OnInit(EventArgs e)
{

if (Request.Url.ToString().Contains("404;"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');

if (urlInfo404.Length > 1)
{
string strRequestUrl = urlInfo404[1].Replace(":" + Request.Url.Port + "/", "/");

if (!strRequestUrl.EndsWith("/"))
{
strRequestUrl = strRequestUrl.Insert(strRequestUrl.Length, "/");
Response.Redirect(strRequestUrl);
}
string[] urlInfoFavAddress = strRequestUrl.Split('/');
string strUser = urlInfoFavAddress[urlInfoFavAddress.Length - 2];

Server.Transfer(string.Concat("~/Profile.aspx?usr=", strUser));

}
}
base.OnInit(e);
}
Here we are just grabbing the trailing string, in this case its imran or armaan.

Once I get the key its get redirected to Profile page, as its Server.Transfer which does not change the url.

Sep 3, 2008

Make temporary redirection to another page

Hello All,

I got one question is asp.net forums, some time people require to change the location automatically or the best example is like, when you shifted your site to new location and you dont want people [who bookmarked your site] request to old site locaiton and get page not found error!

This case what you can do is, you can add automatic redirection to the new site, just adding the meta tag.

You can create one html file or you can write meta tag in older site's default page, here is the meta tag, you just have to place in head tag

<meta http-equiv="refresh" content="5;url='http://knowledgebaseworld.blogspot.com/'" />

http-equiv is the command, which says refresh, so it have to do refresh, and in the content we have to set new locatoin and the time in second. Here its 5 so after 5 second it will automatically redirected to http://knowledgebaseworld.blogspot.com.

We can put some thing like, the page you are looking for its moved, please wait, we are redirecting to new page, book mark this page bla bla bla.....

This way we stop loosing our valuable users of site.

Jun 8, 2008

Working with Extension less URL

Many of us are some time requires to hanlde request which does not contains the extension.

http://yourdomain.com/imran
http://yourdomain.com/knowledge
http://yourdomain.com/world

If you want to achive this functionality then you have to use third party ISAPI module or IIS 6.0 Wildcard mapping feature.

If you goes with ISAPI module, then you should have dedicated server to install that module. And with the other approch of IIS 6.0 Wildcard mapping, its created the proformance problem, as each and every request it tries to apply mapping and degrade the performance.

So in this case the following solutions is good and easy.

What if you made request to a page which out extension? Yes, you will get HTTP 404 page, and IIS are not automatically forward your request to ASP.NET; we can't serve the URL.

So here is the solution,

- Configure the IIS so that; each 404 error page should get redirect to an ASPX page.
- Write the few lines of .NET code.

Thats it.

Lets see how we can do this.

Step 1: Configure IIS for redirection








Step 2: Write the .NET code

You have to capture Application_BeginRequest and put the specilized code to grab 404 url.

protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Url.ToString().Contains("404;"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');

if (urlInfo404.Length > 1)
{
string strRequestUrl = urlInfo404[1].Replace(":" + Request.Url.Port + "/", "/");

//You got the URL now take action on it.
}
}
}

Benefits:

- No third party ISAPI module
- No Wildcard mapping; leads to good profermance.

More on this article? Read the ScottGu's Blog on Tip/Trick: Url Rewriting with ASP.NET.

May 31, 2008

Duplicate key/value pair in Querystring

I was facing a problem, when the Application_AuthenticateRequest get fails and redirects to login page of the system along with the ReturnUrl which contains the actualy requested url.

Problem was like my ReturnUrl contains the same key twice, which leads to crash in system.

Apart from this, I found in Trace that the ReturnUrl is fine at the requested Page.
like... I made request for http://www.imran/MyProfile.aspx?id=102; it shows 302 as [redirected flag] and the ReturnUrl get the value of http://www.imran/MyProfile.aspx?id=102, but when it goes to Login.aspx it shows 2 query string params.

1. ReturnUrl = http://www.imran/MyProfile.aspx?id=102
2. id=102

Here is the problem, we can see the id is part of ReturnUrl, but in this case it generates following redirection url

Login.aspx?ReturnUrl=http://www.imran/MyProfile.aspx?id=102&id=102

Now when I ask for Request["id"], which gives me value comma saperate like 101,102 and this is what we can say a bug.

After searching long I found the solution, which include the Event that we have to capture in Global.asax, and one property of Response object which sets/gets the Http location Header, here is the code.


protected void Application_EndRequest(object sender, EventArgs e)
{
if (Response.RedirectLocation != null && Response.RedirectLocation.Contains("ReturnUrl"))
{
Response.RedirectLocation = string.Format("{0}ReturnUrl={1}",
Response.RedirectLocation.Remove(Response.RedirectLocation.IndexOf("ReturnUrl")
, (Request.RawUrl.Contains("ReturnUrl") ?
Request.RawUrl.Substring(Request.RawUrl.IndexOf("ReturnUrl") + 10)
: Request.RawUrl
)
));
}
}

This will add the RawUrl into ReturnUrl so there will be no duplication.

May 10, 2007

HTTP Response Codes

Hi all,

In the web world we are getting lots of HTTP error, i have delicios which has all the response codes with its description.

Feb 23, 2007

Personalise Your Site's Bookmark [Adding icon in addressbar]

Hello,

I had requirement in which I have to add icon in addressbar, its really simple.

You just have to create one icon file Favicon.ico

Now how to Create a "Favicon.ico" File?

· Create an image 16X16 pixels in size. Yes, it is really small and you can't really draw much in it. You should also restrict yourself to the standard Windows 16 colours, although I suspect that 256 colours will work fine.
· If you like, you can also create a 32X32 pixel icon, which will be scaled to size for the Favorites menu and the location bar. You can even put both 16X16 and 32X32 pixel icons into the same icon file. Windows will use the former for its menus and the latter when the user opens up a folder that is set to display large icons. It's probably not really necessary to do this if you can't be bothered.
· Save the image as an ICO file (named "favicon.ico", of course).
Upload it to your website. You don't need to upload one to every directory of your site if you don't want to waste space - simply put it in your root directory and IE will apparently locate it eventually. You can also upload it into your images directory, but you will need to modify your web pages if you do. See later in this article for more information

Helping the Browser Locate the favicon.ico file
If you have placed your favicon.ico file in a location other than the current web directory (relative to your web page) or the root directory, you have to help your visitors' browser locate the favicon file by specifying it with a tag like the following in the <head> section of your web page.

<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

Feb 10, 2007

Trackback problems!!

Hi,

i had implemented incomming trackback handler.

following may be helpful in implementation.

http://scottwater.com/blog/archive/Trackbacks/
http://www.xaraya.com/documentation/rfcs/rfc0047.html