jer*_*ass 8 c# rest routes asp.net-core-mvc asp.net-core
我正在尝试为我的MVC网络应用程序设置一个API,它将包含很多路由,但每个路由都有相同的部分.基本上每个区域的CRUD.我也将它设置为版本.我已经设置了两个控制器,每个控制器都有一个简单的操作来启动并立即接收冲突.我得到的错误是
我追随这些网址
MVC会让你拥有一个
所以我正在寻找一个不必要地命名的东西,contacts_delete并locations_delete产生像这样的URL
等等
(这是在评论中提供的根本问题的观察结果以及以下类似的情况Route names must be unique. You cannot have two different routes named delete. Use deleteLocation and deleteContact instead.)
我不妨这样做,https://foo.bar/aim/v1/contacts_delete/11111但这对我来说似乎毫无意义.如果MVC可以做到这一点,我必须相信有一种方法可以实现这一点.
我得到的错误是:
具有相同名称"delete"的属性路由必须具有相同的模板:
行动:'rest.fais.foo.edu.Controllers.aimContactsController.delete(rest.fais.foo.edu)' - 模板:'aim/v1/contacts/delete/{id}'
行动:'rest.fais.foo.edu.Controllers.aimLocationsController.delete(rest.fais.foo.edu)' - 模板:'aim/v1/locations/delete/{id}'
对于我设置的控制器
[EnableCors("SubDomains")]
[ResponseCache(NoStore = true, Duration = 0)]
[Produces("application/json")]
[Route("aim/v1/contacts/[action]")]
[ProducesResponseType(typeof(errorJson), 500)]
public class aimContactsController : Controller
{
private readonly IHostingEnvironment _appEnvironment;
private readonly AimDbContext _aim_context;
private readonly UserManager<ApplicationUser> _userManager;
public aimContactsController(IHostingEnvironment appEnvironment,
AimDbContext aim_context,
UserManager<ApplicationUser> userManager)
{
_appEnvironment = appEnvironment;
_userManager = userManager;
_aim_context = aim_context;
}
[HttpPost("{id}", Name = "delete")]
public IActionResult delete(string id)
{
return Json(new
{
results = "deleted"
});
}
}
[EnableCors("SubDomains")]
[ResponseCache(NoStore = true, Duration = 0)]
[Produces("application/json")]
[Route("aim/v1/locations/[action]")]
[ProducesResponseType(typeof(errorJson), 500)]
public class aimLocationsController : Controller
{
private readonly IHostingEnvironment _appEnvironment;
private readonly AimDbContext _aim_context;
private readonly UserManager<ApplicationUser> _userManager;
public aimLocationsController(IHostingEnvironment appEnvironment,
AimDbContext aim_context,
UserManager<ApplicationUser> userManager)
{
_appEnvironment = appEnvironment;
_userManager = userManager;
_aim_context = aim_context;
}
[HttpPost("{id}", Name = "delete")]
public IActionResult delete(string id)
{
return Json(new
{
results = "deleted"
});
}
}
Run Code Online (Sandbox Code Playgroud)
对于Startup.cs我有
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//RolesData.SeedRoles(app.ApplicationServices).Wait();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
//app.UseExceptionHandler("/Home/Error");
}
app.UseIdentity();
app.UseDefaultFiles();
app.UseStaticFiles();
//app.UseResponseCompression();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseSession();
// custom Authentication Middleware
app.UseWhen(x => (x.Request.Path.StartsWithSegments("/aim_write", StringComparison.OrdinalIgnoreCase) || x.Request.Path.StartsWithSegments("/rms", StringComparison.OrdinalIgnoreCase)),
builder =>
{
builder.UseMiddleware<AuthenticationMiddleware>();
});
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "docs";
//c.SwaggerEndpoint("/docs/v1/wsu_restful.json", "v1.0.0");swagger
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1.0.0");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "javascript",
template: "javascript/{action}.js",
defaults: new { controller = "mainline" });
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "AIMApi",
template: "aim/v1/{action}/{id?}",
defaults: new { controller = "aim" });
routes.MapRoute(
name: "AIMContactsApi",
template: "aim/v1/contacts/{action}/{id?}",
defaults: new { controller = "aimContactsController" }
);
routes.MapRoute(
name: "AIMLocationsApi",
template: "aim/v1/locations/{action}/{id?}",
defaults: new { controller = "aimLocationsController" }
);
routes.MapRoute(
name: "RMSApi",
template: "{controller=rms}/v1/{action}/{id?}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法谷歌答案如何解决这个问题.我想解决眼前的问题,但欢迎任何建议.
Fed*_*uma 13
您的两个路由都命名相同,这在ASP.NET Core MVC中不起作用.
我不是在谈论命名方法,而是关于路由命名.您Name = "delete"在HttpPost属性中调用了两个具有相同标识符的路由.MVC中的路由名称唯一标识路由模板.
从我所看到的,你不需要确定你的路线,但只是为了区分不同的URI.因此,您可以自由删除操作方法Name的HttpPost属性属性.这应该足以让ASP.NET Core路由器匹配您的操作方法.
相反,如果您仅使用属性路由进行还原,则最好将控制器更改为以下内容:
// other code omitted for clarity
[Route("aim/v1/contacts/")]
public class aimContactsController : Controller
{
[HttpPost("delete/{id}")]
public IActionResult delete(string id)
{
// omitted ...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11200 次 |
| 最近记录: |