C# 如何使web应用完全独立于web api
我正在开发带有服务的web应用程序,为此我开发了两个不同的项目,一个用于web应用程序,另一个用于web api。现在我陷入困境,因为我只是在我的web应用程序上使用REST服务,但我需要解决一件事,web应用程序需要独立于web api,所以我不希望web api依赖于我的web应用程序 所以问题是,在我的web应用程序文件program.cs和startup.cs中,我从API中调用上下文,但我不知道是否需要它们,现在我被阻止了,因为我需要找到一种方法或知道如何将web应用程序完全独立于web API,但同时像现在一样使用API中的服务 Startup.csC# 如何使web应用完全独立于web api,c#,rest,api,model-view-controller,C#,Rest,Api,Model View Controller,我正在开发带有服务的web应用程序,为此我开发了两个不同的项目,一个用于web应用程序,另一个用于web api。现在我陷入困境,因为我只是在我的web应用程序上使用REST服务,但我需要解决一件事,web应用程序需要独立于web api,所以我不希望web api依赖于我的web应用程序 所以问题是,在我的web应用程序文件program.cs和startup.cs中,我从API中调用上下文,但我不知道是否需要它们,现在我被阻止了,因为我需要找到一种方法或知道如何将web应用程序完全独立于we
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MainContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole> (options => { options.User.AllowedUserNameCharacters = "ãéabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = true; })
.AddEntityFrameworkStores<MainContext>()
.AddDefaultTokenProviders();
services.AddSingleton<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "HomePage" });
});
//new UserRoleSeed(services).Seed().Wait();
}
public static void Main(string[] args)
{
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<MainContext>();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.AddIdentity(options=>{options.User.AllowedUserNameCharacters=“ãabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789-。@+”;options.User.RequireUniqueEmail=true;})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddSingleton();
服务。配置(配置);
//添加应用程序服务。
services.AddTransient();
services.AddMvc();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、IServiceProvider服务)
{
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller}/{action}/{id?}”,
默认值:新建{controller=“Home”,action=“HomePage”});
});
//新建UserRoleSeed(services.Seed().Wait();
}
Program.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MainContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole> (options => { options.User.AllowedUserNameCharacters = "ãéabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = true; })
.AddEntityFrameworkStores<MainContext>()
.AddDefaultTokenProviders();
services.AddSingleton<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "HomePage" });
});
//new UserRoleSeed(services).Seed().Wait();
}
public static void Main(string[] args)
{
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<MainContext>();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
publicstaticvoidmain(字符串[]args)
{
var host=BuildWebHost(args);
使用(var scope=host.Services.CreateScope())
{
var services=scope.ServiceProvider;
尝试
{
var context=services.GetRequiredService();
}
捕获(例外情况除外)
{
var logger=services.GetRequiredService();
logger.LogError(例如,“对数据库进行种子设定时发生错误”);
}
}
host.Run();
}
公共静态IWebHost BuildWebHost(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.Build();
}
如果有人帮助我或对此有所了解,我将非常感谢您的帮助。…如果您有一个REST API,它独立于前端。如果它不是独立的,那么你就没有REST API。@LucasWieloch你是什么意思?如果我错了,请纠正我,但是你的web应用程序是与API一起构建的,并且在这两个API中都使用了公共模块否,web应用程序与web API是独立的,只是使用服务来获取值