1

I have a test controller where i have one index action which accept custid as a argument.

this is how my controller looks

public class TestController : Controller
{
    // GET: Test
    public ActionResult Index(int custid)
    {
        return View();
    }
}

i have added one extra routing statement in route.config file.

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

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

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

so when accessing test controller action with url like localhost:50675/test/101 then getting error. error message say

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

but when accessing test controller action with url like localhost:50675/test?custid=101 then getting no error.

so i do not understand what mistake is there in code.

What i need to do as a result i can issue this url http://localhost:50675/test/101 which should work. please guide me. thanks

1 Answer 1

2

Your route definition need to contain a segment for custid (not id) to match the name of the parameter. The route definition should also contain the name of the controller to make it unique

routes.MapRoute(
    name: "custom1",
    url: "Test/{custid}", // modify
    defaults: new { controller = "Test", action = "Index"}
);

Note that you can also remove the custid = UrlParameter.Optional since you do not want it to be optional

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

5 Comments

sorry it was a typo and very bad mistake. thanks sir.
Sir solution not working. please create a controller at your side which will have one index action with one custid param and do the needful change in routing config file and then run....must see solution not working. i do the changes as per your explanation but now when i am issuing this url localhost:50675/test?101 then getting error "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. "
The code I gave works correctly (I have no idea what other mistakes you have made)
And its url localhost:50675/test/101 to match the route (not test?101)
sorry there was a mistake at my end. now all is working

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.