How to get rid of actual file-name in URL? - asp.net

For instance, this url is http://stackoverflow.com/questions/ask, instead of something like http://stackoverflow.com/questions/ask.aspx. How can I do this?

You can use this tricks and tools:
http://forums.iis.net/t/1150350.aspx - IIS Solution
http://forums.asp.net/t/979581.aspx - ASP.NET 404 Page solution

Add a reference to the assembly System.Web.Routing and then add the following to your global.asax file:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"questions/ask/{question}",
"~/questions/ask.aspx");
}

If you're question is not specific to aspx pages you can do this with a .htaccess file on any webserver running apache (and mod_rewrite - but that's almost all of them).
See various questions on this site about .htaccess or check out many simple tutorials online.
Basically you can put a special file .htaccess in the root of the directory you wish to be affected, that essentially say
Instead of about_us/details.php you can just use about_us/details
This is a very simple example
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^questions/ask ../questions/ask.aspx [L]

This happens when the web server implements a handler that maps a given URL pattern to actual content to serve up.
In this site, it's probably from the use of the MVC framework (guessing).
On windows IIS you can use http://www.iis.net/download/URLRewrite

Related

url rewrite in asp.net for dynamially generated urls

I have few dinamically generated urls like http://localhost:35228/begineercontent?name=lanaguages&id=23, I used routing to hide .aspx extension and now i want to see above url like this http://localhost:35228/begineercontent/lanaguages/23 i tried few url rewrite methods from iis url rewrite tool nothing worked kindly please help me out
This can be done very easy. You need to change/add this code in your global.asax.
void registerroute(RouteCollection routes)
{
routes.MapPageRoute("begineercontent", "begineercontent/languages/{Id}", "~/begineercontent.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
You also need to include the right assembly in global.asax by adding
using System.Web.Routing
In your begineercontent.aspx you need to add this code to the page load function.
var Id=( Page.RouteData.Values["Id"] as string);
A more detailled tutorial on how to work with routes can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/cc668177%28v=vs.100%29.aspx

How to remove folder name from URL in asp .net

Hi I have just shifted my website to new hosting parter with multiple website hosting plan. Every things are working fine except one issue with url. As for different websites there are diffrent folders where i upload my website files these will cause url issues. like :
My previous URL : www.abc.com
After uploading www.abc.com/folderName So I would like to remove folderName from the url. I have googled to many documents regarding web.config url rewrites but none of them are working. Kindly help to remove folderName from the URL. (Note I am also using URL Routing in global.asax if possible to remove folder name using global.asax it will work for me too)
Thanks
I have solved my issue by following steps
1) Used Global.asax to forefully redirect my users to my website without folder name.
protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.RedirectPermanent("http://www.myDomain.com");
}
2) I have changed my relative URLs to Absolute URLs
Changed ~/contact to http://www.myDomain.com/contact

ASP.NET Routing in Global.asax

I'm trying to add a route in my web forms application by following this:
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application
I've added the route in my Global.asax file like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "/WebsiteName/{combinedPin}", "~/Default.aspx");
}
I then try to visit my website locally like this:
http:// localhost:12345/WebsiteName/test36u
But I get a resource cannot be found message so I don't think my route is correct. Can anybody see a problem with my code?
Any pointers would be much appreciated.
Thanks
You do not need to specify the name of your website as part of the route, try with this code:
routes.MapPageRoute("", "{combinedPin}", "~/Default.aspx");
With the above code, your link would look like:
http://localhost:12345/WebsiteName/test36u
If however your intention is that your users access your site using a segment named: WebsiteName then use:
routes.MapPageRoute("", "WebsiteName/{combinedPin}", "~/Default.aspx");
But in the precedent code your users will have to access your resource as follows: (probably not the expected result though)
http://localhost:12345/WebsiteName/WebsiteName/test36u

using dll extention instead of the standard aspx

I am replacing an existing web application, that all it's requests go through a url:
www.something.com/scripts/xxx.dll?args
I created my own aspx page that handles these requests and it is called:
www.something.com/scripts/xxx.aspx?args
My problem is that there are many existing links, from other website that refer to the xxx.dll?args url.
Can I create my own dll in .net that will receive the xxx.dll?args requests and process them?
This isn't a simple redirect, because I also need the args
I'd suggest to rather use Url Rewriting
After some more investigation I did the following.
Change the web.config, to make sure all requests go through my code by adding the following code:
...<system.webServer>
<modules runAllManagedModulesForAllRequests="true">...
Added a global.asax file to the web project, and within it wrote the following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Path.EndsWith("xxx.dll",
StringComparison.InvariantCultureIgnoreCase))
Context.RewritePath("/scripts/xxx.aspx");
}

asp.net site default document in subfolder

My default document is in subfolder not in root how can i make it default in asp.net 2.0 website.
Tried iis7 default document setting to '/pages/default.aspx'
'~/pages/default.aspx' but it didn't work.
Default document is not the same as start page. Default document means if I requested mysite.com/somefolder and didn't specify a file, which file should IIS display.
If you want to use a specific page as your home page, create a Default.aspx file and write this in it's codebehind class:
public override void ProcessRequest(HttpContext context) {
context.Response.Redirect("pages/default.aspx", true);
}
As the client might have disabled Javascript, a server side approach would be more reliable. However it's best to issue a permanent redirect instead of a simple Response.Redirect. Also doing it using JS will be bad from a SEO point of view.
You don't need to create a dummy Default.aspx page.
In your Global.asax.cs file, write the following:
public void Application_Start(object sender, EventArgs e)
{
var routeCollection = RouteTable.Routes;
routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx");
}
Explanation:
Application_Start code is guaranteed to run once and only once on the application start.
The first line of code, gets a collection of the URL routes for your application.
The second line of code, defines a new route pointing to your inner page in the subfolder that you wish.
The second argument is empty to indicate that this route is used when there's no specific page is requested and there's no Default document existing.
Default documents are a subfolder-specific thing - what you're trying to do won't (directly) work. Set up a default.htm file in the root, and have it refresh to your real "home page".
The better question you should be asking is how on Earth your homepage got out of the root directory.
In theory you could have a Web.config file inside the directory and use the defaultDocument element to set the default document. See here: https://stackoverflow.com/a/2012079/125938.
Unfortunately I haven't been able to get it to work myself locally, but that might be because it isn't supported in the Visual Studio development server.
Say "index.html" is the default page you want and it is present in "Public" subfolder.
Instead of specifying "/Public/index.html" as the default site, try "Public/index.html"

Resources