0
votes

I have created my first WebAPI project to learn, which had an index.html page in the root of the project. I set that page as Startup. All working fine. But, I want to use an MVC controller to call the View instead.

So I created a new MVC controller in my Controller folder called "DefaultController". In it, there's a method:

 public ActionResult Index()
        {
            return View();
        }

I created a View folder, and off that, a Default folder, in which, I created an Index.cshtml file.

When I start the project, it calls my old index.html. So, I changed the startup to be the index.cshtml, which is wrong - know. MVC calls a controller method. So, I'm trying to work out - how do I call the controller method in my DefaultController?

I think I need to change my routes?

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

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

        }

My plan is to use cshtml pages (instead of html pages) to make use of layouts and allow controllers to initiate the views. Each view will the use an api call back to my WebApi controllers to do the data handling.

Does that seem like a good way to handle my WebAPI/KnockoutJs project?

I just need to know how to get the controller to be the default.

When removing the index.html page, I get the error:

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

3

3 Answers

0
votes

I think you need to add the controller name to the defaults object as in:

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

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

You need to register both WebAPI routes and MVC routes: All this should be done in Application_Start method in Global.asax.cs file:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

GlobalConfiguration.Configure(WebApiConfig.Register) is used to configure WebApi (and register api related routes)():

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

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

RouteConfig.RegisterRoutes(RouteTable.Routes); is used to register MVC routes:

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

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

You also need verify that the DefaultController that you created is MVC controller (inherits from System.Web.Mvc.Controller) and not WebAPI controller

0
votes

According to my experience, when you want to call the index.cshtml, in the route config you have to define the controller like this in the RouteConfig.cs:

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

The controller supposed to be "Home", and the Action is "Index". But, this maproute is for default one. How about if you want to add another one?

   routes.MapRoute(
      "Article",
      "articles",
      new { controller = "News", action = "ArticleList" }
   );

You can write freely as shown above where "Articles" is the name of maproute and the "articles" is the url. And it would become like this (http://www.domain.com/articles) if you compile controller News and Action ArticleList. And "..../articles" is something you replace (No need define controller or action) and you don't need to open www.domain.com/News/ArticleList it's enough to go to url www.domain.com/articles and the maproute would be automaticaly route to controller news and action articlelist.

That's only my point of view about how maproute works.

CMIIW :)