简体   繁体   English

如何在C#/ ASP.NET中维护正确的URL?

[英]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". 但是,当它试图调出页面时,我(通过IE)得到“此页面无法显示”。

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 网址很明显是错误的,因为首先我看到一些东西http://www.Something.org/Search.aspx,然后它变成了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? 我应该怎么做才能避免使用静态URL并使其动态化,以使其始终指向正确的域?

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. 在控制器中使用HttpContext.Current.Request.Url查看URL。 Url contains many things including Host which is what you're looking for. Url包含许多内容,包括您要查找的Host

By the way, if you're using the latest .Net 4.6+ you can create the string like so: 顺便说一句,如果您使用的是最新的.Net 4.6+,则可以这样创建字符串:

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

Or you can use string.Format 或者您可以使用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 如果您使用的是.Net 4.6+,则还可以使用此字符串插值版本

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) 您可以将Host段存储在Web.Config文件的AppSettings部分中(按类似的config /环境)

Debug / Development Web.Config 调试/开发Web.Config

在此处输入图片说明

Production / Release Web.Config (with config override to replace the localhost value with something.org host) 生产/发布Web.Config(使用config覆盖将localhost值替换为something.org主机)

在此处输入图片说明

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. Uri类具有许多有用的属性和方法,可以为您提供Relative UrlAbsoluteUrl ,您的Url FragmentsHost名等。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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