0

I know there's another thread about WebapiConfig.cs and RouteConfig.cs, but I can assure this is a different question.

My MVC project was quite developed by the time I found out I would have to create a webservice (in the same domain) where I would grant access to one of my models (both in JSON and XML).

In order to do so, I right clicked over my Controllers and selected "add Web API 2 Controller with actions, using Entity Framework" and selected also my model class and db context.

The template was quite complete and I thought I was ready to go for the following test:

namespace MVC4GMAPS.Controllers
{
    public class RestController : ApiController
    {
        private LocationDBContext db = new LocationDBContext();

        // GET api/Rest
        public IQueryable<Location> GetLocations()
        {
            return db.Locations;
        }
etc...

Unfortunately, when I tried to access to https://localhost:44300/api/Rest I've got a 404. However, localhost:44300/Home/Index keeps working great.

I believe the problem relies on my RouteConfig.cs, because it is expecting an {action} and my RestController doesn't have any actions:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
etc...

What can I do? Can I create simultaneously a WebapiConfig.cs file? I believe not. I hope you can help me!

1 Answer 1

4

Do you also have a WebApiConfig.cs? In a project which was "Web API from the start" I have this in it:

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.MapHttpAttributeRoutes();

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

This, like the other configs, is called from Global.asax.cs:

GlobalConfiguration.Configure(WebApiConfig.Register);

That should get the routing pointed to the API controller. Otherwise, as you say, the route defined from MVC by default would be looking for an action called Rest on a controller called api which doesn't exist.

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

3 Comments

@LuisGouveia: Looks like it's defined in the assembly System.Web.Http.WebHost, if you don't have a reference to that you'll need to add one. It's possible that the project template originally used didn't reference some assemblies that WebAPI currently takes for granted, so there could be a little trial-and-error in identifying and adding them.
I removed my question because I figured it out myself. I thank you a lot but even with the new WebApiConfig and the renewed global.asax I keep on getting a 404 when I try /api/rest...
@LuisGouveia: Interesting. I wonder if the route table is being modified in some other way, or if there's perhaps something else that a WebAPI project does outside of this code. Might be worth creating a new from-scratch project with WebAPI in the template from the start and seeing if you can replicate the issue. This might not be it, but another thing I'm seeing is that your method is called GetLocations whereas I thought WebAPI just expected Get. (If it's that simple, that would be awesome.)

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.