为何将参数传递给我自己的Startup类? [英] Hw to pass arguments to my own Startup class?
问题描述
我正在尝试使用OWIN开发一个Web API自托管应用程序. 在我自己的XyzStartup类中,我需要一个外部参数:contentFolderPath.
I'm trying to develop a web api self hosting app using OWIN. In my own XyzStartup class, I need an external argument: contentFolderPath.
但是,我没有找到传递这种说法的方法.这是我的代码如下:
However, I didn't find a way to pass this argument. Here is my code below:
var contentFolderPath = this.TextBox.Text; // user input
var startOptions = new StartOptions();
using(WebApp.Start<XyzStartup>(startOptions)){
}
我的创业公司
public class XyzStartup
{
XyzStartup(string contentFolderPath) { ... }
}
我注意到有一个StartOption类,但是没有如何使用它.我可以在我的XyzStartup类中使用它吗?
I noticed there is a StartOption class, but don't how to use it. Can I use it in my XyzStartup class?
提前谢谢!
我终于找到了一种方法:
I finally find a way to do this:
var startOptions = new StartOptions();
startOptions.Urls.Add('..some url ..');
WebApp.Start(startOptions, (appBuilder)=>{
new XyzStartup(contentFolderPath).Configuration(appBuilder);
}
推荐答案
如果要将参数传递给StartUp类,则可以在WebApp中使用Action<IAppBuilder>
.像注释中提到的CilliéMalan一样启动,而不是使用Type参数启动(WebApp.Start<T>
)
If you want to pass parameter to StartUp class, you can use Action<IAppBuilder>
in WebApp.Start like Cillié Malan mentioned in the comment instead of launching with Type parameter(WebApp.Start<T>
)
以下是自我托管的具体示例:
Here is a concrete example for self-hosting:
object someThingYouWantToAccess;
var server = WebApp.Start("http://localhost:8080/", (appBuilder) =>
{
// You can access someThingYouWantToAccess here
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
});
这篇关于为何将参数传递给我自己的Startup类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!