How can I redirect and change urls without extension via ASP.NET?

We redesigned the structure into a website with several business units. Now I want to redirect (301) to a new page.

IE:
was www.example.com/abc
now www.example.com/default.aspx?article=abc

I tried using Global.asax for this and it works correctly when I debug it.

if (Request.RawUrl.Contains("abc")) { Response.RedirectLocation = "/default.aspx?article=abc"; Response.StatusCode = 301; Response.StatusDescription = "Moved"; Response.End(); } 

So http: // localhost: 1234 / example / abc redirects correctly, but (where 1234 is the port for the debug server)
http: // localhost / example / abc does not redirect, it gives me 404.

Any ideas?


Additional information: If I go to http: //localhost/example/abc/default.aspx , then it will be redirected correctly.

+4
source share
8 answers

Well, if the port indicates that you are using the embedded web server (the one that comes with VS), this probably works because it always routes requests through the ASP.NET infrastructure.

Requests ending in / abc will not automatically be routed through the ASP.NET infrastructure because IIS may not “know” what you want. You need to check your IIS settings to make sure such requests are redirected to aspnet_isapi.dll


EDIT: For this you need to add a wildcard mapping

  • In IIS Manager, expand the local computer, expand the Web Sites folder, right-click the website or virtual directory that you want, and select Properties.
  • Select the appropriate tab: Home Directory, Virtual Directory, or Directory.
  • In the Application Settings area, click Configuration, and then click the Mappings tab.
  • To install a wildcard application map, follow these steps:
    • On the Mapping tab, click Add or Paste.
    • Enter the path to the DLL in the Executable file text box or click Browse to go to it (for example, the ASP.NET 2.0 DLL is located in the c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi. Dll directory on my machine)
    • For extension use ". *" Without quotes, of course
    • Choose which verbs you want to search for (GET, HEAD, POST, DEBUG are common for ASP.NET, you decide)
    • Make sure "Script engine" or "Application engine" is selected.
    • Uncheck "Check if file exists"
    • Click OK.

I can be on this, but if I find, I hope someone will correct me. :)

+6
source

You should use wildcard IIS redirection, you will need something like this:

  *; www.example.com/*; www.example.com/default.aspx?article=$0 

There is a reasonable link to Microsoft

If you are using Apache, I think you will need to modify the htaccess file.

+2
source

Are you currently testing the site on the Visual Studio web server? Usually he starts the site on "localhost: nnnnn", where "nnnnn" is the port number (as indicated above 1234), he does not start it without it.

If you have IIS installed on the appropriate computer, publish your project on it, and you can make sure that it works without "nnnnn", since there will be nothing in your code that could cause it to not do this.

+1
source

Make sure the web.config files are the same for each website (assuming: 1234 is different from: 80)

Also, have you tried localhost: 80?

0
source

Your http: // localhost / example / abc does not call Global.asax as you expect. Typically, http: // localhost runs on port 80 (: 80). If you want to run your site on port 80, you will need to deploy your site in IIS to run here.

0
source

You need to configure Handler mapping in IIS to forward all unknown extensions to asp.net. The first one works because cassini handles all requests, the second one does not work, because IIS looks for this directory and it does not exist, not the .net framework that works with your code.

Here is information on how to do Url Rewriting in asp.net .

If possible, I would suggest using the new Application Request Routing or UrlRewrite.net

0
source

IIS does not by default pass all requests to ASP.NET for processing. Only some resource extensions, including "aspx", will be passed to asp.net for processing. What happens when you request http: // localhost / example / abc is that IIS tries to find the directory to find out if you have a default file (i.e. default.aspx, index.html) to load from this directory. Since he cannot find the directory with the "abc" tag in it, he never finds the default.aspx file to load.

When you try to download http: //localhost/example/abc/default.aspx , IIS sees the aspx extension and immediately passes it during ASP.NET execution for processing. The reason the request http: // localhost / example / abc does not load is because it is never passed to ASP.NET, so of course global.asax never sees this.

A site hosted on Cassini handles all requests, so the call is handled by ASP.NET and the global.asax file.

I agree with Darren Kopp, who suggested that you need to configure Handler mapping in IIS to forward unknown ASP.NET extensions.

0
source

Source: https://habr.com/ru/post/1277078/


All Articles