简体   繁体   English

ASP.Net MVC auto slugify Routes

[英]ASP.Net MVC auto slugify Routes

i have this code in my Controller:我的控制器中有此代码:

string APIBaseurl = "https://sub.domain.de/";
    [Route("VerseExkurs/Technologien/{technologie}")]
    public async Task<ActionResult> Details(string technologie)
    {
        TechnologieRootobject TechnologieInfo = new TechnologieRootobject();

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri(APIBaseurl);

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            HttpResponseMessage Res = await client.GetAsync("items/technologien/" + technologie + "?access_token=token");

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api   
                var TechnologieResponse = Res.Content.ReadAsStringAsync().Result;

                //Deserializing the response recieved from web api and storing into the Employee list  
                TechnologieInfo = JsonConvert.DeserializeObject<TechnologieRootobject>(TechnologieResponse);

            }
            //returning the employee list to view  
            return View(TechnologieInfo.data);
        }
    }

But the Problem is, the "{technologie}" Variable is in some Cases with Spaces.但问题是,“{technologie}”变量在某些情况下带有空格。 Is there any way to automatically convert spaces in underdashes?有没有办法自动转换下划线中的空格?

You have few options.你有几个选择。

  1. Utilize string.Replace() method使用 string.Replace() 方法
    HttpResponseMessage Res = await client.GetAsync("items/technologien/" + technologie.Replace(" ", "_") + "?access_token=token");
  1. Write a helper function that returns correct string编写一个返回正确字符串的辅助函数
string removeSpaceFromString(string inputString)
{
     return inputString.Replace(" ", "_"); // or some other method of doing that
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM