码迷,mamicode.com
首页 > 移动开发 > 详细

[转]How can I get my webapp's base URL in ASP.NET MVC

时间:2014-07-14 23:24:22      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:http   os   art   for   io   cti   

本文转自: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 here 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 really depends on how often you need to use it... if this is a single use deal then just put it in the class where you 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 Statics and I put functions like the above there...

just make sure you change the above from public string GetBaseUrl() to public static string GetBaseUrl()

[转]How can I get my webapp's base URL in ASP.NET MVC,布布扣,bubuko.com

[转]How can I get my webapp's base URL in ASP.NET MVC

标签:http   os   art   for   io   cti   

原文地址:http://www.cnblogs.com/freeliver54/p/3842416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!