JavaEar 专注于收集分享传播有价值的技术资料

How can an OWIN Startup method get the base URL of the web site?

How can an OWIN startup method get the base URL of the web site?

I'm trying to write code that will work when debugging with IISExpress, unit testing with self hosting and under IIS.

When self hosting, I can find this information in IAppBuilder.Properties["host.Addresses"] but, it isn't there when running under IISExpress (haven't checked IIS).


  1. System.Web.VirtualPathUtility.ToAbsolute("~")
  2. 参考答案2
  3. For anyone under vNext, try this:

    public void Configure(IApplicationBuilder app)
                app.Use(async (ctx, next) =>
                    var hostingEnvironment = app.ApplicationServices.GetService<IHostingEnvironment>();
                    var realPath = hostingEnvironment.WebRootPath + ctx.Request.Path.Value;
                    // do something with the file
                    await next();

    If you're not under vnext, I did see this answer that did not work for me under vNext / dnx:


    Under dnx this just game me the folder containing the .dnx runtime but might work under other contexts

  4. 参考答案3
  5. Please don't downvote this anymore post. This is not an accepted answer. I am keeping this post as an educational to illustrate not to go down the path I provided. It's easy to see how this answer could have been a solution to an untrained eye.

    (EDIT: 10/6/2016)

    Okay, so the conversation below this post was not helping me understand why I was wrong. So I've asked friends, coworkers, and finally received a good answer from the local community that I'm part of to explain the conversation more and why this answer wasn't sufficient. It was mentioned that the below does NOT answer the start up application request of the base URL, it answers how to retrieve fetching the base URL of the requested application using request handling. The difference is, the request handling fetches the base URL upon every request; whereas fetching the base URL from the start up application occurs only once and keeps the value of the requested application only when the application begins.

    I honestly haven't seen or viewed any kind of documentation that allows you to retrieve the base URL outside of the current request handling scheme. I'm also not sure if it's even possible in the current state of the .NET stack. So again, I do apologize for not pointing this out and getting confused about the solution.

    For those of you who still want to use the workaround in fetching the base url from a requested application (which may be from the startup application.. or some other kind of external application), and don't mind fetching it per request, and doesn't mind what point later in time that the base URL will be retrieved, the solution below answers that.

    (Original Solution)

    Here's a nice article that explains using Owin middleware pipeline:

    You can use app.Run which accept a context, or app.Use which accepts a context, and next which is of type Func (getting the next step in the pipeline).

    public void Configuration(IAppBuilder app)
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
        //First way using app.Use
        var currentUri1 = "";
        app.Use((context, next) => { 
            currentUri1 = context.Request.Uri.ToString(); //Get base URL
            return next().ContinueWith(task =>
                context.Response.WriteAsync(" FINISHED RETRIEVING CURRENT URL ");
        //Second way using app.Run
        var currentUri2 = "";
        app.Run((context) => { 
            currentUri2 = context.Request.Uri.ToString(); //Get base URL
             var task = context.Response.WriteAsync("Hello world! " + context.Request.Path);
                return task;

    context.Request is essentially the wrapper of the incoming request, it's of type IOwinRequest. More info is found here: IOwinRequest Interface

    The Uri property of IOwinRequest is of type System.Uri, so you can view what kind of properties (like host, port, absolute url, query variables, etc) that come with Uri detailed here: Uri Class

    [EDIT in response to comment]

    If you truly don't believe that context.Request is available in startup, check out this flow:

    IOwinContext, which "wraps OWIN environment dictionary and provides strongly typed accessors", which has a property called IOwinRequest, which "Gets a wrapper exposing request specific properties." It's of type Microsoft.Owin.IOwinRequest, which essentially has the property Uri