Go How can I get my WebApp ' s base URL in ASP.

Source: Internet
Author: User

This article transferred from: HTTP://STACKOVERFLOW.COM/QUESTIONS/1288046/HOW-CAN-I-GET-MY-WEBAPPS-BASE-URL-IN-ASP-NET-MVC

Maybe it is extension or modification of the answers posted this but I use simply following and it works:

Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~")

When my path is:http://host/iis_foldername/controller/action

Then I receive:http://host/iis_foldername/

public string GetBaseUrl()
{
    var request = HttpContext.Current.Request;
    var appUrl = HttpRuntime.AppDomainAppVirtualPath;

    if(!string.IsNullOrWhiteSpace(appUrl)) appUrl += "/";

    var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);

    return baseUrl;
}

That's really depends on what often you need to use it ... if this is a, single, deal then just put it in the class where yo U need this data,

If you anticipate using it in multiple classes in your app, then I use a folder called Helpers in the base of my app,

I have a static class called and Statics I put functions like the above there ...

Just make sure-the above from to public string GetBaseUrl()public static string GetBaseUrl()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.