MVC 6 - API API 2.0 blank space on parameter URL

I am trying to search the web API url as follows:

http://localhost:8000/api/trips/World Trip/stops

In this case, the word "World Trip" is the word. But when the call arrives at the server, it arrives as follows:

enter image description here

" World%20Trip" with code %20to replace empty space!

Is there any tweak that needs to be made to prevent code from replacing space? I remember <httpRuntime relaxedUrlToFileSystemMapping="true" />in previous versions.

I do not want to use any method for conversion inside the server: for example HttpServerUtility.UrlEncode().

My details Route summary:

[Route("api/trips/{tripName}/stops")]

My StopController.cs

using AutoMapper;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using TheWorld.Models;
using TheWorld.ViewModels;

namespace TheWorld.Controllers.Api
{
    [Route("api/trips/{tripName}/stops")]

    public class StopController : Controller
    {
        private ILogger<StopController> _logger;
        private IWorldRepository _repository;

        public StopController(IWorldRepository repository, ILogger<StopController> logger)
        {
            _repository = repository;
            _logger = logger;
        }

        [HttpGet("")]
        public JsonResult Get(string tripName)
        {
            try
            {
                var results = _repository.GetTripByName(tripName);

                if (results == null)
                {
                    return Json(null);
                }

                return Json(Mapper.Map<IEnumerable<StopViewModel>>(results.Stops.OrderBy(s => s.Order)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get stops for trip {tripName}", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Error occurred finding trip name");
            }
        }
    }
}
+4
source share
3 answers

HttpUtility.UrlDecode(yourstring), .

+1

, " - ASP.net 5, MVC 6, EF7 AngularJS".

, ,

tripName = WebUtility.UrlDecode(tripName);

, , -8. rc1, .

+1

user1919597 ASP rc1-update1 ( 11/30 ). . Shawn Blog Post

, URL-,

return Json($"Error occurred finding trip name {tripName}");

, tripName url - : http://localhost:5151/api/trips/World%20Trip/stops " World Trip".

Now, if you only get an error message, not data, then something else may happen ... that happens to be the problem that I am now facing this part of the course ... it was 2 days searching without a true fix ...

Everything and everything - start by updating everything in accordance with the Shawn Blog, and then check it again. Good luck, and I’m interested to know how it turns out.

+1
source

Source: https://habr.com/ru/post/1616908/


All Articles