Webapi Controller - Routing not working

106 views Asked by At

i have problem with my webapi controller, can not make it work correctly....

What is wrong?

Here the code:

WeabiConfig.cs

        EnableCrossSiteRequests(config);

        config.MapHttpAttributeRoutes();

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

        // this i added
        config.Routes.MapHttpRoute(
            name: "Action",
            routeTemplate: "api/{controller}/{action}"
         );

MyController:

    [ActionName("TransformXMLToHTML")]
    [System.Web.Http.AcceptVerbs("GET")]
    [System.Web.Http.HttpGet]
    public IHttpActionResult TransformXMLToHTML()
    {
        string xsltString  = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("~/Xml/inputXslt.xslt"));
        string inputXml = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("~/Xml/inputXml.xml"));
        XslCompiledTransform transform = new XslCompiledTransform();
        using (XmlReader reader = XmlReader.Create(new StringReader(xsltString)))
        {
            transform.Load(reader);
        }
        StringWriter results = new StringWriter();
        using (XmlReader reader = XmlReader.Create(new StringReader(inputXml)))
        {
            transform.Transform(reader, null, results);
        }
        return Ok(results.ToString());
    }

GET Request: http://localhost:60674/api/comments/TransformXMLToHTML

Error: No action was found on the controller 'Comments' that matches the request

Thanks guys!

1

There are 1 answers

0
Russ Cam On

Route matching happens from top to bottom meaning that the first route that matches the given request url will be returned. In this case, the request is matching the "api/{controller}/{key}" route which does not specify an a default action.

To rectify, either remove that route, or add some Route constraints to it to ensure that the request in question does not match it.