I have the following program that shows JSON data.
Code of Controller
:
using MVCCuartoDia2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCCuartoDia2.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public JsonResult getJson()
{
Customer objCustomer = new Customer();
objCustomer.CustomerCode = "c001";
return Json(objCustomer, JsonRequestBehavior.AllowGet);
}
public ActionResult MostrarJson()
{
return View("AprenderJquery");
}
}
}
Class Customer
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCCuartoDia2.Models
{
public class Customer
{
private string _CustomerCode;
public string CustomerCode
{
get { return _CustomerCode; }
set { _CustomerCode = value; }
}
}
}
View AprenderJquery
:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AprenderJquery</title>
<script src="@Url.Content("~/scripts/jquery-1.10.2.js")"></script>
</head>
<body>
<div>
<input type="button" value="ver dato JSON" onclick="return getJson()" />
</div>
<script type="text/javascript">
function getJson()
{
$.getJSON("/Customer/getJson", null, Display1)
return true;
}
function Display1(data)
{
alert(data.CustomerCode);
}
</script>
</body>
Code of Routeconfig
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCCuartoDia2
{
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 = "Customer", action = "MostrarJson", id = UrlParameter.Optional }
);
}
}
}
When I run the project he says:
Server error in the application '/'. The resource is not found. Description: HTTP 404. The resource you are looking for (or one of your dependencies) may have been removed, changed names or not be available temporarily Check the following URL and make sure it is spelled correctly.
URL requested: /
However both the name of the Controller
and the Action
are well placed, why does not it work for me?