7

I wrote this code in a C# ASP.NET Core Web API project:

[HttpGet]
[Route("GetShortURL/{_url}/{tokenPass}")]
[ApiExplorerSettings(GroupName = "ShortURL")]
public ActionResult<ServiceResult<string>> GetShortURL(string _url, string tokenPass)

When I enter this parameter as _url, I get an error:

Error: Not Found

https://github.com/VahidN/DNTPersianUtils.Core

http://...//GetShortURL/https%3A%2F%2Fgithub.com%2FVahidN%2FDNTPersianUtils.Core/TokenPass

How can I call this API with the first Web URL parameter? when i change the [Route("GetShortURL/{_url}/{tokenPass}")] to [Route("GetShortURL")] the problem was solved but i want to send query by / not by ? for example, i want to call API like this :

1- http://..../GetShortURL/_UrlParam/_TokenPassParam

not like below :

2- http://..../GetShortURL?_url=_urlParam&tokenPass=_TokenPassParam

the second way works fine but I want first way to work correctly when i pass an URL like this

https%3A%2F%2Fgithub.com%2FVahidN%2FDNTPersianUtils.Core

can anyone help me?

14
  • 2
    Sorry, what error are you getting? Commented Jul 4, 2022 at 12:03
  • Error: Not Found Commented Jul 4, 2022 at 12:04
  • Is there any route prefix? Commented Jul 4, 2022 at 12:08
  • no , i define it in attribute ... [Route("GetShortURL/{_url}/{tokenPass}")] Commented Jul 4, 2022 at 12:13
  • 1
    Please edit the question to add any extra detail/clarity rather than in the comments. Commented Jul 4, 2022 at 13:49

3 Answers 3

2

First approach: Pass the params you want as query string and then change the method like below:

[HttpGet("GetShortURL")]
[ApiExplorerSettings(GroupName = "ShortURL")]
public ActionResult<ServiceResult<string>> GetShortURL(string _url, string tokenPass)

Then For extracting the different parts of the url (protocol, domain name, path and query string), use the code below (path is an array separated by slash):

try
{
    var decodedUrl = System.Web.HttpUtility.UrlDecode(_url);
    Uri uri = new Uri(decodedUrl);    
    var scheme = uri.Scheme;            
    var host = uri.Host;
    var absolutePathSeperatedBySlash = uri.AbsolutePath.Split('/').Skip(1).ToList();
    var query = uri.Query;
    // rest of the code ...
     
}
catch (Exception ex)
{
    //...
}

Second approach:

If you want it to be sent as a url parameter, first you have to encode the value of _url with encodeURIComponent() in javascript, to make sure that some special characters like , / ? : @ & = + $ # are changed.

Then:

[HttpGet("GetShortURL/{_url}/{tokenPass}")]
[ApiExplorerSettings(GroupName = "ShortURL")]
public ActionResult<ServiceResult<string>> GetShortURL(string _url, string tokenPass)

The rest is just like the method body of the first approach.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you - I think that I can't say my question correctly--- so I edit that
I edited the answer based on what you asked, hope it is useful.
0

With the following url

http://...//GetShortURL/https%3A%2F%2Fgithub.com%2FVahidN%2FDNTPersianUtils.Core/TokenPass

The value of _url will be: enter image description here

If you want to convert it to a correct url,you needs to replace %2F with / in GetShortURL:

var url = _url.Replace("%2F","/");

3 Comments

thank you but this is not the answer of my question...
You can use the second way to pass url and replace %2F with /.
this URL https%3A%2F%2Fgithub.com%2FVahidN%2FDNTPersianUtils.Core automatically converted to github.com/FVahidN/FDNTPersianUtils.Core and i don't need to use replace method .. . my question is that when I use the first way this can not call my API
0

just make parameters to be optional

[HttpGet("GetShortURL/{_url?}/{tokenPass?}")]
public ActionResult<ServiceResult<string>> GetShortURL(string _url, string tokenPass)

in this case you can call the action without any parameters, with one parameter or with two parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.