[转]How can I get my webapp's base URL in ASP.NET MVC
2014-07-14 11:50
495 查看
本文转自: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:
When my path is:
then I receive :
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
I have a
just make sure you change the above from
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
Helpersin the base of my app,
I have a
staticclass called
Staticsand I put functions like the above there...
just make sure you change the above from
public string GetBaseUrl()to
public static string GetBaseUrl()
相关文章推荐
- How to create UrlSlug in Asp.Net MVC
- [转载]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- How to use HTML-5 data-* attributes in ASP.NET MVC
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL(转)
- How to bootstrap two angular apps in one asp.net mvc application
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- How to get URL and QueryString value in an ASP.NET page
- How to achive the CRUD in ASP.NET MVC(VS2010)
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- How to create custom html helper in Asp.net MVC 3 or 4
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
- how to get all controller attribute action and parameter in asp.net mvc
- Can we call the Method of a controller from another controller in asp.net MVC?