我怎样才能得到网站的baseURL时? [英] How can I get the baseurl of site?

查看:180
本文介绍了我怎样才能得到网站的baseURL时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写返回该网站的基本URL一个小的辅助方法。这是我想出了:

I want to write a little helper method which returns the base url of the site. This is what I came up with:

public static string GetSiteUrl()
        {
            string url = string.Empty;
            HttpRequest request = HttpContext.Current.Request;

            if (request.IsSecureConnection)
                url = "https://";
            else
                url = "http://";

            url += request["HTTP_HOST"] + "/";

            return url;
        }

有没有这一点,你能想到的任何错误。你可以根据这个改进?

Is there any mistake in this that you can think of. Can you improve upon this?

推荐答案

试试这个:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/";

这篇关于我怎样才能得到网站的baseURL时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆