The resource is not found in c #

0

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?

    
asked by Andrés Oporto 04.05.2017 в 05:02
source

1 answer

2

Your problem is here:

$.getJSON("/Customer/getJson", null, Display1);

Solution:

var url = '@Url.Action("getJson", "Customer")';
$.getJSON(url, null, Display1);

Whenever you use URLs in JavaScript that point to resources in ASP.net MVC , use the @Url.Action and @Url.Content because that way the framework is responsible for generating the routes from where you are going to put in production your system.

If you are starting with ASP.net MVC, I did a workshop of 4 hours 4 minutes and 4 seconds in link which contains the source code in Github. Take classes in link where they teach you very cheeere and they are free, I definitely recommend it.

    
answered by 04.05.2017 / 19:19
source