0

I have these controller methods in my web api, both of them accept HttpPost. The Process() method receives a complex type parameter in the body. The Install method receives a string parameter in the body. The Process method is called successfully, but the Install method fails with error 404 - not found, I assume the routing is failing, but I just can't figure out what am I doing wrong...

        [HttpPost]
        [ResponseType(typeof(IProcessableObject))]
        [Route("Workflow/Process")]
        public IHttpActionResult Process([FromBody]SerializedObject request)
        {
            try
            {
                Type objectType = ResolveType(request.ObjectType);
                IProcessableObject obj = (IProcessableObject)JsonSerializer.Deserialize(request.RawObject, objectType);
                log.DebugFormat("Processing {0} with workflow {1}", objectType.Name, obj.WorkflowId);
                var workflow = workflowController.Get(obj.WorkflowId, true);
                var workflowProcessor = new WorkflowProcessor(obj, workflow);
                if (workflowProcessor.Process())
                    return Ok(obj);
                return InternalServerError();
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Failed processing object {0}", request.ObjectType), ex);
                return InternalServerError();
            }
        }

        [HttpPost]
        [ResponseType(typeof(int))]
        [Route("Workflow/Install/{userName}")]
        public IHttpActionResult Install(string userName, [FromBody]string xmlTemplate)
        {
            try
            {
                log.DebugFormat("User {0} is installing new workflow:{1}{2}", userName, Environment.NewLine, xmlTemplate);
                var wf = workflowController.Install(xmlTemplate, userName);
                if (wf == null)
                    return BadRequest();
                return Ok(wf.WorkflowId);
            }
            catch (Exception ex)
            {
                log.Error("Failed installing workflow", ex);
                return InternalServerError();
            }
        }

And from my MVC application I call them like this:

        public static IProcessableObject Process(IProcessableObject obj, bool isProxy = false)
        {

            string requestURL = string.Concat(wfServiceUrl, "Workflow/Process");
            var requestData = new SerializedObject
            {
                RawObject = JsonSerializer.Serialize(obj),
                ObjectType = isProxy ? obj.GetType().BaseType.AssemblyQualifiedName : obj.GetType().AssemblyQualifiedName
            };

            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                var result = client.UploadString(requestURL, JsonSerializer.Serialize(requestData));
                return (IProcessableObject)JsonSerializer.Deserialize(result, isProxy ? obj.GetType().BaseType : obj.GetType());
            }
        }

        public static int Install(string workflowTemplate, string userName)
        {
            string requestURL = string.Concat(wfServiceUrl, "Workflow/Install/", userName);


            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                var result = client.UploadString(requestURL, JsonSerializer.Serialize(workflowTemplate));
                return JsonSerializer.Deserialize<int>(result);
            }
        }



public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }
    }
Gratziani V.
  • 127
  • 3
  • 12
  • Are you using .net Core framework ? – Rahul Nov 15 '18 at 16:00
  • I guess check your parameters order for Install method. – Wit Wikky Nov 15 '18 at 16:08
  • I'm not using .Net Core, it's .Net standard. Tried to change the order of the parameters in the controller method, but no success, the same thing keeps happening. It seems my post methods only work when there is only a complex type argument posted (See Process() method). If the argument is string as in the Install() method, it fails – Gratziani V. Nov 15 '18 at 16:24
  • @GratzianiV. show the WebApiConfig and also show if there are any prefixes on the controller. – Nkosi Nov 15 '18 at 16:49
  • I have updated the post with the web api config. What do you mean by prefixes on the controller? I have no atrributes on the controller class... – Gratziani V. Nov 15 '18 at 17:01
  • check to make sure that the proper URL is being called. Make sure `userName` is included in the URL from the client. Debug and step through the client. What is the value of `useName` when you get 404 – Nkosi Nov 15 '18 at 17:14
  • Ok so I figured out what the issue is... But I'm not sure how to fix it... The problem is that the userName had a "." character. When I remove it, it works. Now I tried encoding the userName value like this:string requestURL = string.Concat(wfServiceUrl, "Workflow/Install/", HttpUtility.UrlEncode(userName)); But still failing. Shouldn't UrlEncode do the trick? – Gratziani V. Nov 16 '18 at 07:55
  • Found the fix on another thread. THe solution is to add a trailing slash in the URL, because otherwise the server consider's it's a file, instead of a call: https://stackoverflow.com/questions/13298542/apicontroller-returns-404-when-id-contains-period – Gratziani V. Nov 16 '18 at 08:01
  • Found the answer here: https://stackoverflow.com/questions/13298542/apicontroller-returns-404-when-id-contains-period – Gratziani V. Nov 16 '18 at 08:02

1 Answers1

0

Try changing this:

[Route("Workflow/Install/{userName}")]

For this:

[Route("api/Workflow/Install/{userName}")]

And do the same with your other routes, add api/ and that should work.