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

后端 未结 3 1530
盖世英雄少女心
盖世英雄少女心 2021-02-12 15:57

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

3条回答
  •  清酒与你
    2021-02-12 16:34

    For anyone under vNext, try this:

    public void Configure(IApplicationBuilder app)
            {
                app.Use(async (ctx, next) =>
                {
                    var hostingEnvironment = app.ApplicationServices.GetService();
                    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: https://stackoverflow.com/a/24747658/486028

    Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)

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

提交回复
热议问题