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.
Your problem is (at least when I tried your code) you have route constraints specified where they really shouldn't be. I was able to get this to work just fine by doing:
routes.MapRoute(
"Product",
"{controller}/{deli}/{productId}",
new { controller = "emlak", action = "Index", productId = UrlParameter.Optional, deli = UrlParameter.Optional }
);
Try that - any difference?
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.
This may help, as I haven't upgraded from MVC 1.0 yet...
I don't think you need the .aspx portion of the URL because MVC handles application instantiation differently. Also you need a .mvc extension if using IIS 6 (e.g. "emlak.mvc/TITLE/number"); IIS 7 should instantiate correctly with "emlak/TITLE/number".
You should remove the constraints and provide the deafults for "productId" and "deli".
routes.MapRoute(
"Product",
"{controller}/{deli}/{productId}",
new { controller = "emlak", action = "Index", productId = 123 , deli = "xyz" }
);
OR
make your parameters optional at action in your controller
public ActionResult Index(String productId = 0, String deli = "")
{
return View();
}
Your action requires that deli and productId both be supplied, and your route does not supply default values for either. Either add an Index action which does not require any values be supplied, or add default values for your variables.
counsellorben