1

I'm new to .NET and C#, having trouble routing to my one view file on app start. There must be something I don't understand about the routing, because the URL in the browser:

/Views/LoadCustomerAndDisplay/Index.cshtml

seems right to me. However, I get a 404 error when I build and run the app. Here is a picture of the file structure:

enter image description here

and here is the browser error:

enter image description here

RouteConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication6
{
    public class RouteConfig
    {
        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 }
            );
        }
    }
}

LoadCustomerAndDisplayController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class LoadCustomerAndDisplayController : Controller 
    {
        // GET: LoadCustomerAndDisplay
        public ActionResult Index()
        {
            Customer objCustomer = new Customer();
            objCustomer.Id = 1001;
            objCustomer.CustomerCode = "C";
            objCustomer.Amount = 900.78;
            return View(objCustomer);
        }
    }
}

what am i missing here?

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Adam Weitzman
  • 1,552
  • 6
  • 23
  • 45

2 Answers2

2

You cannot access the .cshtml via the browser. The correct URL would be /LoadCustomerAndDisplay/Index. It uses the Index action of the LoadCustomerAndDisplay controller.

As a side note, you probably want to create a more generic controller name like Customers and create any actions relating to your customer entities under that controller.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Sam
  • 9,933
  • 12
  • 68
  • 104
0

In MVC, Routing maps virtual "pages" to physical files, and the default route is {controller}/{action}/{id}.

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.

So in your case, you are calling /views/LoadCustomerAndDislpay/index.cshtml but with MVC you don't reference physical files, but instead call {controller}/{action}.

Call:

/LoadCustomerAndDisplay

and it should work. (The Index action is the default and can be omitted).


This assumes you haven't made any changes to your RouteConfig, it should be something like:

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

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
    }

I am assuming that your LoadCustomerAndDisplay controller looks something like:

public class LoadCustomerAndDisplayController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

My convention, the controller should be nammed xxxController and looking at your file name, that may not be the case.

From asp.net:

Notice that the first part of the controller name is highlighted in the Add Controller dialog. Every controller name must end with the suffix Controller. For example, you can create a controller named ProductController but not a controller named Product.

(admittedly from an older version but its the best version of the quote I could find)


Finally, the View should be in a folder, called the same as the controller, without the controller suffix, so in your case LoadCustomerAndDisplay.

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117