首页 > 移动平台 > 详细

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

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

本文转自: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://www.cnblogs.com/freeliver54/p/3842416.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!