ASP.NET项目ConfigureServices报CS0161错误,求解决方案
你遇到的CS0161错误原因很明确——ConfigureServices方法声明了返回object类型,但方法体内没有任何return语句,导致代码路径没有返回值。我们一步一步来解决这个问题,顺便优化你的Startup配置,避免后续的其他坑:
1. 修复CS0161错误
在ASP.NET Core中,ConfigureServices方法的默认返回类型是void(不需要返回任何值),你错误地把它写成了object。只需要修改方法的返回类型即可:
// 把原来的public object ConfigureServices(...)改成: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DataContext>(s => s.UseSqlServer("ConnectionString")); services.AddControllers(); // 下面这行有冗余问题,后面会说明 // services.AddMvc(services => services.EnableEndpointRouting = false); }
2. 修正连接字符串的配置问题
你现在硬编码了"ConnectionString",这显然无法连接到真实数据库,应该从配置文件(appsettings.json)中读取实际的连接字符串:
首先确保你的appsettings.json里有数据库连接字符串:
{ "ConnectionStrings": { "DefaultConnection": "Server=你的数据库服务器名;Database=你的数据库名;Trusted_Connection=True;TrustServerCertificate=True;" } }
然后修改AddDbContext的代码,从配置中读取连接字符串:
services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
3. 移除冗余的Mvc配置(避免路由冲突)
在ASP.NET Core 3.0及以上版本,AddControllers()已经包含了Web API所需的核心服务,不需要再调用AddMvc。同时你的Configure方法里同时调用了UseEndpoints和UseMvc,这会导致路由冲突——UseMvc会覆盖端点路由的配置。建议保留现代的端点路由方式,移除UseMvc:
修改后的Configure方法:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); // 只保留端点路由配置即可 app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // 移除这一行:app.UseMvc(); }
4. 额外建议:提前配置CORS(适配Angular跨域通信)
因为你要和Angular前端跨域通信,后续肯定需要配置CORS,提前在ConfigureServices里添加CORS服务可以避免后续踩坑:
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); // 添加CORS策略,允许来自Angular默认端口的请求 services.AddCors(options => { options.AddPolicy("AllowAngularApp", policy => policy.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod()); }); services.AddControllers(); }
然后在Configure方法里,在UseRouting之后添加UseCors:
app.UseRouting(); app.UseCors("AllowAngularApp"); // 添加这一行,启用CORS策略 app.UseAuthorization();
修改后的完整Startup.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using WebAPI_1.Data; using Microsoft.EntityFrameworkCore; namespace WebAPI_1 { public class Startup { 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<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddCors(options => { options.AddPolicy("AllowAngularApp", policy => policy.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod()); }); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseCors("AllowAngularApp"); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
这样不仅解决了CS0161错误,还为后续和Angular的跨域通信做好了准备,同时修正了配置中的潜在问题。
内容的提问来源于stack exchange,提问作者Pan Michal

