【问题标题】:Adding configurations to manually created startup.cs class in asp.net web api project向asp.net web api项目中手动创建的startup.cs类添加配置
【发布时间】:2020-07-08 09:22:52
【问题描述】:

我是 asp.net 的新手,并从 Visual Studio 中的一个 Web API 项目开始。我想将这些 Web API 用于 Angular 应用程序。为此,我必须启用 CORS 并配置 SignalR

In the ConfigureServices method, we are adding SignalR to the IService collection. And in a Configure 
method, we are adding SignalR to the request pipeline by pointing to our ChartHub with the provided /chart path.

参考 - “https://code-maze.com/netcore-signalr-angular/”。

我的 Web API 项目中没有Startup.cs 文件,所以我添加了一个OWIN startup class。这个类有一个Configuration 方法,但没有ConfigureService 或Configure。我对如何在startup.cs 中添加启用 CORS 和 SignalR 感到困惑。有人可以帮助我如何在 startup.cs 类中添加这些配置吗?

【问题讨论】:

  • 听起来您没有从正确的项目类型开始。你确定这是你选择的 .net core API 项目吗?
  • 我不想要 .net 核心项目。我创建了一个 asp.net Web 应用程序(.NET Framework)
  • .NET Framework 项目没有startup.cs,你发的参考是.net core的。
  • 好的。如果.NET Framework项目没有startup.cs,应该在哪里添加CORS之类的配置?
  • 发布的答案可能是您最好的选择,但是 TBH,当 .net 5 出现时(很快),我认为您将没有太多选择。 .NET 核心是未来,因此您应该以核心启动任何新项目并尽快转换旧项目。

标签: c# asp.net asp.net-web-api startup .net-framework-version


【解决方案1】:

您需要在项目的根目录中添加 Startup 类,并用 OwinStartup 属性标记它以指示要用于自动设置的类。

然后您需要实现配置方法,您可以在其中设置身份验证、COR、Signalr 等内容。

您也可以通过在 Visual Studio 中添加项目来添加 Owin 启动类。它将为您添加基本设置到配置。 https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection#create-an-aspnet-web-app-using-owin-startup

[assembly: OwinStartup(typeof(Example.Web.Startup))]
namespace Example.Web
{
    public class Startup
    {
        private static CorsOptions _corsOptions = new CorsOptions
        {
            PolicyProvider = new CorsPolicyProvider
            {
                PolicyResolver = context =>
                {
                    var policy = new CorsPolicy();
                    foreach (string domain in ConfigHelper.AllowedCorsDomains.Split(','))
                    {
                        policy.Origins.Add(domain);
                    }

                    policy.AllowAnyMethod = true;
                    policy.AllowAnyHeader = true;
                    policy.SupportsCredentials = true;
                    return Task.FromResult(policy);
                }
            }
        };

        public void Configuration(IAppBuilder app)
        {
            app.UseCors(_corsOptions);
            app.MapSignalR();

            AccessTokenFormat = new SecureDataFormat<AuthenticationTicket>(new TicketSerializer(), app.GetDataProtectionProvider().Create("ASP.Net Identity"), new Base64TextEncoder());

            
            OAuthBearerAuthenticationOptions = new OAuthBearerAuthenticationOptions()
            {
                AccessTokenFormat = AccessTokenFormat
            };

            if (provider != null)
            {
                OAuthBearerAuthenticationOptions.Provider = provider;
            }

            app.UseOAuthBearerAuthentication(OAuthBearerAuthenticationOptions);
        }
    }
}

【讨论】:

  • 我收到一个错误ConfigHelper 在当前上下文中不存在。
  • 嗯 ConfigHelper 用于从 web.config 获取配置。对于这个特定的地方,它正在获取启用 cors 的域的逗号分隔列表。如果您不想将域存储在 web.config 中,您可以将 ConfigHelper 行替换为“yourdomain1,yourdomain2”字符串
猜你喜欢
  • 2015-10-28
  • 2018-11-11
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多