Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

owin

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).

like image 379
John Vottero Avatar asked Apr 28 '15 17:04

John Vottero


People also ask

What is the use of OWIN startup class?

Every OWIN Application has a startup class where you specify components for the application pipeline. There are different ways you can connect your startup class with the runtime, depending on the hosting model you choose (OwinHost, IIS, and IIS-Express).

Where do you typically specify components for the application pipeline in an OWIN app?

OWIN uses a startup class where you can specify the components you wish to include in the application pipeline. If you look at the Katana source code, the Katana SystemWeb host uses the PreApplicationStartMethodAttribute to hook into the application startup.


2 Answers

System.Web.VirtualPathUtility.ToAbsolute("~")
like image 82
SergeyA Avatar answered Oct 27 '22 02:10

SergeyA


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:

http://blog.2mas.xyz/owin-middleware/

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

like image 43
sksallaj Avatar answered Oct 27 '22 01:10

sksallaj