0

i'm getting this error when i'm navigate browser to url:

localhost:10793/RealEstates/10

this my RouteConfig code:

    public class RouteConfig
    {
       public static void RegisterRoutes(RouteCollection routes)
       {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Main", action = "Index" }
        );
        routes.MapRoute(
            name: "RealEstates",
            url: "RealEstates/{action}",
            defaults: new { controller = "RealEstates", action = "Index" }
        );
        routes.MapRoute(
            name: "RealEstatesViewAd",
            url: "RealEstates/{id}",
            defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
        );
    }

}

my error:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

when changed code to:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}",
        //    defaults: new { controller = "Main", action = "Index" }
        //);
        //routes.MapRoute(
        //    name: "RealEstates",
        //    url: "RealEstates/{action}",
        //    defaults: new { controller = "RealEstates", action = "Index" }
        //);
        routes.MapRoute(
            name: "RealEstatesViewAd",
            url: "RealEstates/{id}",
            defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
        );
    }

}

it's work but when i call on other actions in controller

localhost:10793/RealEstates/CreateAd

this error found

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewAd(Int32)' in 'Youe3lan.Controllers.RealEstatesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

and this my controller:

    namespace MvcAppliction1.Controllers
{    
    public class RealEstatesController : Controller
    {        
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ViewAd(int id)
        {
            return View();
        }
        public ActionResult CreateAd()
        {
            return View();
        }
    }
}
2
  • Put your two RealEstate routes ahead of the Default route. Commented Apr 12, 2013 at 15:51
  • 1
    You want to put your most specific routes first. If you put your most general routes first they'll always be hit and your specific routes will be ignored Commented Apr 12, 2013 at 15:52

2 Answers 2

1

You need to change it to:

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

Have a look here it might help:

http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx

UPDATE

Add this to your controller:

public ActionResult ViewAd(Int32 id)
{
    return View();
}

You see

localhost:10793/RealEstates/10

is translated to:

localhost:10793/RealEstates/ViewAdd/10

So you need that method in the controller accepting an it parameter.

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

3 Comments

Please do not add link only answers, meta.stackexchange.com/questions/8231/…
i'm tried this but it dosen't work when i'm call >> localhost:10793/RealEstates/10 but other action has worked
Of Course it fails because you don't have an handler for that in your controller. You need to add: see update
0

you've flagged your id in your route as optional:

id = UrlParameter.Optional

but I bet your controller isn't nullable??

public ActionResult ViewAd(Int32 id)
{

}

So you cant post a null into your id even though the route allows it. If you change this to:

public ActionResult ViewAd(Int32? id)
{

}

You won't get the error message:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewAd(Int32)' in 'Youe3lan.Controllers.RealEstatesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

2 Comments

When I call this action with localhost:10793/RealEstates/10 it work but when i call other action will navigate to this action ViewAd()
I think you need a combination of my fix and @Marco below.

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.