C# – ASP.NET MVC 3 routing

asp.net-mvcasp.net-mvc-3asp.net-mvc-routingcnet

i am trying to create route.

Which is

/emlak/TITLE/number.aspx

such as

/emlak/Here_is_your_best_property/123456.aspx

Global.asax:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new { controller = "emlak", action = "Index" },
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

My controller

namespace emrex.Controllers
{
    public class EmlakController : Controller
    {
        //
        // GET: /Emlak/

        public ActionResult Index(String productId, String deli)
        {
            return View();
        }

    }
}

and i am getting next error:

Server Error in '/' Application.

The resource cannot be found.

Thanks for help.

Best Answer

Don't provide URL parameter defaults as constraints (as you did)

When you define your route as (I added additional comments so we know what each part is)

routes.MapRoute(
    // route name
    "Product",

    // Route URL definition
    "{controller}/{deli}/{productId}",

    // route values defaults
    new { controller = "emlak", action = "Index" },

    // route values constraints
    new { productId = UrlParameter.Optional , deli = UrlParameter.Optional  }
);

So basically you shouldn't provide constraints in your case which makes it meaningless. Put the last two in route defaults and keep constraints out of this route defintion as:

routes.MapRoute(
    "Product",
    "{controller}/{deli}/{productId}",
    new {
        controller = "Emlak",
        action = "Index",
        productId = UrlParameter.Optional,
        deli = UrlParameter.Optional
    }
);

This should definitely work unless you have some other route definitions or don't use code that you provided.