1

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.

Community
  • 1
  • 1
Engin
  • 79
  • 1
  • 9
  • I think its a typo, but your post says the route is being mapped in the web.config. It should be global.asax. Also nEEbz has a good suggestion, you should include your action method as a code snippet, it will help us diagnose the problem – Scott Lance Apr 22 '11 at 15:08
  • also do you have a controller named emlak with a method named index? drop the .aspx, its not very 'resftul' and isnt standard for mvc unless you 'need it' for some reason (iis 5.1?) – Adam Tuliper Apr 22 '11 at 15:10
  • Have you tried with either lowercase controller name (`emlakController`) or uppercase route controller value default (`controller = "Emlak"`)? – Robert Koritnik Apr 23 '11 at 08:13
  • And please **provide the URL address** you're trying to access when getting that particular error. Is it just "/" or is it something else? – Robert Koritnik Apr 23 '11 at 08:14
  • plz have a look at this [question](http://stackoverflow.com/questions/5732507/asp-net-mvc-2-issue-dot-in-route/5732908#5732908). it contains a similar problem and answers may help you – Muhammad Adeel Zahid Apr 22 '11 at 15:48
  • Ask question and go away. It is not good... – Nuri YILMAZ Nov 30 '11 at 17:25

5 Answers5

4

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.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
3

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?

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • what version of IIS are you running on? I tried your exact code (but with my route above) in IIS 5.1 on XP with MVC3 and it worked. – Adam Tuliper Apr 23 '11 at 05:28
  • 1
    @Engin - if you are using the built in web server in visual studio, make sure in your project properties you dont have a "Virtual Path" set, as it could be affecting your URI's path that the web server is expecting to see in front of your url, throwing off the pattern which could then also cause a 404. – Adam Tuliper Apr 23 '11 at 17:25
1

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".

JoshNaro
  • 2,047
  • 2
  • 20
  • 40
  • 1
    .mvc isn't required for iis6, you can use a wildcard mapping: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx – Adam Tuliper Apr 22 '11 at 15:10
1

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();
}
adyusuf
  • 806
  • 1
  • 11
  • 27
0

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

counsellorben
  • 10,924
  • 3
  • 40
  • 38