80

I know that I can use Url.Link() to get URL of a specific route, but how can I get Web API base URL in Web API controller?

4

16 回答 16

94

In the action method of the request to the url "http://localhost:85458/api/ctrl/"

var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) ;

this will get you http://localhost:85458

于 2015-04-21T15:12:54.793 回答
47
Url.Content("~/")

worked for me!

于 2015-10-29T01:36:40.153 回答
44

You could use VirtualPathRoot property from HttpRequestContext (request.GetRequestContext().VirtualPathRoot)

于 2013-11-08T18:41:43.687 回答
16

This is what I use:

Uri baseUri = new Uri(Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.PathAndQuery, String.Empty));

Then when I combine it with another relative path, I use the following:

string resourceRelative = "~/images/myImage.jpg";
Uri resourceFullPath = new Uri(baseUri, VirtualPathUtility.ToAbsolute(resourceRelative));
于 2014-01-24T09:24:54.800 回答
12

In .NET Core WebAPI (version 3.0 and above):

var requestUrl = $"{Request.Scheme}://{Request.Host.Value}/";


     
于 2020-08-03T17:01:32.193 回答
10

I inject this service into my controllers.

 public class LinkFactory : ILinkFactory
 {
    private readonly HttpRequestMessage _requestMessage;
    private readonly string _virtualPathRoot;


    public LinkFactory(HttpRequestMessage requestMessage)
    {
        _requestMessage = requestMessage;
        var configuration = _requestMessage.Properties[HttpPropertyKeys.HttpConfigurationKey] as HttpConfiguration;
        _virtualPathRoot = configuration.VirtualPathRoot;
        if (!_virtualPathRoot.EndsWith("/"))
        {
            _virtualPathRoot += "/";
        }
    }

    public Uri ResolveApplicationUri(Uri relativeUri)
    {

        return new Uri(new Uri(new Uri(_requestMessage.RequestUri.GetLeftPart(UriPartial.Authority)), _virtualPathRoot), relativeUri);
    }

}
于 2013-11-09T00:08:42.343 回答
7

Use the following snippet from the Url helper class

Url.Link("DefaultApi", new { controller = "Person", id = person.Id })

The full article is available here: http://blogs.msdn.com/b/roncain/archive/2012/07/17/using-the-asp-net-web-api-urlhelper.aspx

This is the official way which does not require any helper or workaround. If you look at this approach is like ASP.NET MVC

于 2014-06-12T07:45:58.087 回答
5
new Uri(Request.RequestUri, RequestContext.VirtualPathRoot)
于 2015-06-25T20:41:33.057 回答
4

In ASP.NET Core ApiController the Request property is only the message. But there is still Context.Request where you can get expected info. Personally I use this extension method:

public static string GetBaseUrl(this HttpRequest request)
{
    // SSL offloading
    var scheme = request.Host.Host.Contains("localhost") ? request.Scheme : "https";
    return $"{scheme}://{request.Host}{request.PathBase}";
}
于 2019-03-01T19:06:16.363 回答
1

Not sure if this is a Web API 2 addition, but RequestContext has a Url property which is a UrlHelper: HttpRequestContext Properties. It has Link and Content methods. Details here

于 2014-05-12T03:22:30.713 回答
1

First you get full URL using HttpContext.Current.Request.Url.ToString(); then replace your method url using Replace("user/login", "").

Full code will be

string host = HttpContext.Current.Request.Url.ToString().Replace("user/login", "")
于 2018-04-16T07:34:37.107 回答
1

Base on Athadu's answer, I write an extenesion method, then in the Controller Class you can get root url by this.RootUrl();

public static class ControllerHelper
{
    public static string RootUrl(this ApiController controller)
    {
        return controller.Url.Content("~/");
    }
}
于 2018-06-04T09:06:46.993 回答
0

send a GET to a page and the content replied will be the answer.Base url : http://website/api/

于 2015-11-29T01:58:05.787 回答
0
  1. Add a reference to System.Web using System.Web;

  2. Get the host or any other component of the url you want string host = HttpContext.Current.Request.Url.Host;

于 2019-09-15T07:56:07.360 回答
-1

From HttpRequestMessage

request.Headers.Host
于 2015-09-18T09:26:23.810 回答
-1

Al WebApi 2, just calling HttpContext.Current.Request.Path;

于 2016-02-05T14:57:27.243 回答