简体   繁体   中英

How to maintain the right URL in C#/ASP.NET?

I am given a code and on one of its pages which shows a "search result" after showing different items, it allows user to click on one of records and it is expected to bring up a page so that specific selected record can be modified.

However, when it is trying to bring up the page I get (by IE) "This page cannot be displayed".

It is obvious the URL is wrong because first I see something http://www.Something.org/Search.aspx then it turns into http://localhost:61123/ProductPage.aspx

I did search in the code and found the following line which I think it is the cause. Now, question I have to ask:

What should I do to avoid using a static URL and make it dynamic so it always would be pointing to the right domain?

string url = string.Format("http://localhost:61123/ProductPage.aspx?BC={0}&From={1}", barCode, "Search");

Response.Redirect(url);

Thanks.

Use HttpContext.Current.Request.Url in your controller to see the URL. Url contains many things including Host which is what you're looking for.

By the way, if you're using the latest .Net 4.6+ you can create the string like so:

string url = $"{HttpContext.Current.Request.Url.Host}/ProductPage.aspx?BC={barCode}&From={"Search"}";

Or you can use string.Format

string host = HttpContext.Current.Request.Url.Host;
string url = string.Format("{0}/ProductPage.aspx?BC={1}&From={2}"), host, barCode, "Search";

This should do it.

string url = string.Format("ProductPage.aspx?BC={0}&From={1}", barCode, "Search");
Response.Redirect(url);

If you are using .Net 4.6+ you can also use this string interpolation version

string url = $"ProductPage.aspx?BC={barcode}&From=Search";
Response.Redirect(url);

You should just be able to omit the hostname to stay on the current domain.

You can store the Host segment in your AppSettings section of your Web.Config file (per config / environment like so)

Debug / Development Web.Config

在此处输入图片说明

Production / Release Web.Config (with config override to replace the localhost value with something.org host)

在此处输入图片说明

and then use it in your code like so.

            // Creates a URI using the HostUrlSegment set in the current web.config
        Uri hostUri = new Uri(ConfigurationManager.AppSettings.Get("HostUrlSegment"));

            // does something like Path.Combine(..) to construct a proper Url with the hostName 
            // and the other url segments. The $ is a new C# construct to do string interpolation
            // (makes for readable code)
        Uri fullUri = new Uri(hostUri, $"ProductPage.aspx?BC={barCode}&From=Search");

            // fullUrl.AbosoluteUri will contain the proper Url 
        Response.Redirect(fullUri.AbsoluteUri);

The Uri class has a lot of useful properties and methods to give you Relative Url , AbsoluteUrl , your Url Fragments , Host name etc etc.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM