Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass in long parameter to asp.net webapi

Tags:

c#

asp.net-mvc

I'd like to pass a website parameter to a webapi, but it fails to work.

Webapiconfig:

 config.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { id = RouteParameter.Optional }
       );

Web api controller: take the param and return the htmlsnapshot of the other website

    [HttpGet]
    [Route("api/snapshot/{param}")]
    public string GetSnapShot(string param)
    {

        string fragment = param;
        string content = "";

        if (fragment != null)
        {
            int firstSlash = fragment.IndexOf("/");
            if (firstSlash <= 2)
                fragment = fragment.Substring(firstSlash + 1, fragment.Length - firstSlash - 1);
            using (IWebDriver driver = new PhantomJSDriver())
            {
                string url = String.Format("http://domain.com/{0}", fragment);
                driver.Navigate().GoToUrl(url);

                content = driver.PageSource;
            }
        }
        return content;
    }

if I try api/snapshot/du-lieu -> hit the controller fine but if I pass in a more complicated like

api/snapshot/%2Fdu-lieu%2Fbong-da-y-Serie-A%2Fseason%2F1%2Ftong-quan -> fails to work, return 404

Please advise.

like image 421
nam vo Avatar asked Nov 10 '22 13:11

nam vo


1 Answers

Why dont you put your param into query string?? Your code will become

[HttpGet]
[Route("api/snapshot")]
public string GetSnapShot(string param1,string param2,string param3)
{
} 

And from wherever you call the api, create the request url http://<whatever domain you use>/api/snapshot?param1=valueparam1&param2=valueparam2&param3=valueparam3

like image 159
Martin Valentino Avatar answered Nov 14 '22 22:11

Martin Valentino