2

I have a Web Api project which needs to return an MVC styled View. I have made my MVC Controller as such

public class MVCController: Controller
{
    [HttpGet]
    [Route("api/mvc/test")]
    public ActionResult test()
    {
        return View();
    }
}

However, When i try to access this controller from the web, i cant seem to reach the controller. I get the following error:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/foo/api/mvc/test'.","MessageDetail":"No type was found that matches the controller named 'mvc'."}

After doing searches on google, people seem to be telling me to change routing properties in the webapiconfig to

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

But i still seem to be having no luck. IF HOWEVER i change my controller to an webapi controller as such

public class MVCController: ApiController
{
    [HttpGet]
    [Route("api/mvc/test")]
    public IHttpActionResult test()
    {
        return Ok();
    }
}

I can reach the controller.. If someone could give me some insight to as what is going on, it would be much appreciated.

UPDATE After reading the response below, I have updated my controller to look like this:

public class MVCController: Controller
{
    [HttpGet]
    public ActionResult test()
    {
        return View();
    }
}

However, localhost/MVCController/test still seems to give me a 404 error and the Controller is not being hit. Sorry for my newbieness by the way.

1
  • and the api was just testing because i thought that formatting the mvc controller the same way as the web-api would help. It was a desperate measure LOOL Commented Apr 7, 2016 at 16:12

1 Answer 1

2

I bet you have a file called WebApiConfig.cs which has this code.

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 }
        );
    }
}

This is where the routing pattern is defined for the web api controllers. So any request coming with api/something will be considered as a request for web api endpoint because you must have invoked the WebApiConfig.Register call before registering the routes for mvc controllers. The Order of registering routes really matters.

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);   // first one    
    RouteConfig.RegisterRoutes(RouteTable.Routes);    //second one

}

Since the order of registration matters, when a request comes with api/something, it will be matched against the route registration for web apis and framework will try to look for matching web api controller.s

If you swap the order of calling the route registration method, your request will work. but that might affect other parts when you try to access web api controllers.

BTW, Are you sure you want to have api/ in the route pattern for your MVC controller ? Unless you really want some different url patten than the normal convention(*controllername/action*), just remove the Route attribute it self. With the default route definition, it will work for yourSite/mvc/test request.

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

8 Comments

youre right omg I did have the config.MapHttpAttributeRoutes();.
Is the solution to remove it?
Just remove api from the route of your mvc controller.
I still cant seem to reach the controller. I am now prompted with a HTTP Error 404.0 - Not Found Error
Why do you even have the route attribute? your route seems to be same as the normal convention (except the api part which is causing trouble). See my updated answer.
|

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.