asp.net-core-routing

ASP.Net Core Web API custom route not working

白昼怎懂夜的黑 提交于 2021-02-08 19:36:31
问题 I have a ASP.Net Core Web API project, and the following controller: [Route("/api/v0.1/controller")] [ApiController] public class MyController : ControllerBase { [HttpGet("/test")] public ActionResult Test() { return null; } } but when I run the project, at /api/v0.1/controller/test I get "page not found" and I don't see where I made a mistake. 回答1: Your method route template contains prefixed / , due to that, the app route couldn't find the appropriate path. Modify test method route template

How to change URL Suffix of .net core?

隐身守侯 提交于 2021-01-28 03:31:31
问题 How can I have the following route: http://localhost:4413/abc/ And the following route: http://localhost:4413/abc.html both return the same controller method, using .NET Core MVC? 回答1: It sounds like you want two suffixes to reach the same controller action. an empty suffix an .html suffix Here is one way to do that in your Startup.Configure method. Edit its app.UseMvc to look like this: app.UseMvc(routes => { // this is the default route that is already present routes.MapRoute( name:

How to change URL Suffix of .net core?

霸气de小男生 提交于 2021-01-28 02:23:34
问题 How can I have the following route: http://localhost:4413/abc/ And the following route: http://localhost:4413/abc.html both return the same controller method, using .NET Core MVC? 回答1: It sounds like you want two suffixes to reach the same controller action. an empty suffix an .html suffix Here is one way to do that in your Startup.Configure method. Edit its app.UseMvc to look like this: app.UseMvc(routes => { // this is the default route that is already present routes.MapRoute( name:

Can't bind params using [FromQuery] ASP Net Core 2 API

青春壹個敷衍的年華 提交于 2021-01-27 18:36:14
问题 I'm new in ASP Net Core 2, I want to bind different parameters that come from URL query string to action parameters in my action: [HttpGet("{page}&{pageSize}&{predicate}", Name = "GetBuildingsBySearchCriteria")] public IActionResult GetBuildingsBySearchCriteria([FromHeader] string idUser, [FromQuery]int page, [FromQuery]int pageSize, [FromQuery]string predicate) { .... } When I test my action using postman, I set the idUser in header and other parameters in URL, example: http://localhost

Can't get HTTP PUT request to work in ASP.NET Core

爷,独闯天下 提交于 2021-01-20 09:08:18
问题 I'm trying to update an entry in the game table. However, my PUT request in ASP.NET never seems to trigger, and I can't figure out why. This is controller in ASP.NET: [Route("game/{update.GameID}")] [HttpPut] public IActionResult updateGame([FromBody]Game update) { var result = context.Games.SingleOrDefault(g => g.GameID == update.GameID); if (result != null) { result = update; context.SaveChanges(); } return Created("", result); } And this is the code I use in Angular: url:string;

Can't get HTTP PUT request to work in ASP.NET Core

倖福魔咒の 提交于 2021-01-20 09:07:29
问题 I'm trying to update an entry in the game table. However, my PUT request in ASP.NET never seems to trigger, and I can't figure out why. This is controller in ASP.NET: [Route("game/{update.GameID}")] [HttpPut] public IActionResult updateGame([FromBody]Game update) { var result = context.Games.SingleOrDefault(g => g.GameID == update.GameID); if (result != null) { result = update; context.SaveChanges(); } return Created("", result); } And this is the code I use in Angular: url:string;

Route to different actions with the same url with different params

落花浮王杯 提交于 2021-01-04 05:56:12
问题 I'm building api with such routes: /items and /items/{id} . For now I want to route this routes to two different actions. I am not able to configure it with attributes, here is config: routes.MapRoute( "route1", "/items", new { controller = "Items", action = "Get" }); routes.MapRoute( "route2", "/items/{id}", new { controller = "Items", action = "Get" }); But this route just doesn't work. Where am I wrong? 回答1: It's not possible to have 2 action methods with the same name and map them using