【发布时间】:2023-03-06 05:50:01
【问题描述】:
我对 ASP.NET Core 完全陌生,我搜索了很多,但仍然对 Core 2.1 中的路由感到困惑。
所以,我创建了一个示例项目,选择 API 作为模板,VS 创建了如下内容:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseMvc();
}
但我不需要 MVC 提供的所有功能,因为我的项目不使用视图。
任何帮助将不胜感激
【问题讨论】:
-
您可以简单地使用
services.AddMvcCore,然后只添加您真正需要的服务。app.UseMvc部分只是放入已注册的中间件中,因此您不需要在那里做任何特别的事情。尽管有这个名字,但它不仅仅是 MVC 特定的,所以你仍然需要以某种形式调用。 -
我鼓励你去看看每一个的来源。在那里您将能够看到每个调用实际上正在执行的操作,即服务
AddMvc注册与AddMvcCore等github.com/aspnet/Mvc -
@ChrisPratt 感谢您的建议。我查看了 AddMvc,发现了不需要的东西(剃刀视图等)。
标签: c# asp.net-core asp.net-core-mvc asp.net-core-2.0