5

I am working on a very simple application, using MVC2 Preview 1.

I have a controller named ContentController. My problem is that /Content/Index works correctly, but /Content/ returns a 404. I am running the application on the Studio Development Server.

Tested with RouteDebugger but /Content/ returns a 404, and does not display any debugging information.

I have not changed the routing code:

       routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

This is my controller:

public class ContentController : Controller
{
    IRepository _repo = new SimpleRepository("db", SimpleRepositoryOptions.RunMigrations);

    public ActionResult Index()
    {
        var content = _repo.GetPaged<Content>(0, 20);
        return View(content);
    }
4
  • Are you running this under IIS or the visual web server? If IIS, which version? Do you get the same result with /Content (vs. /Content/)? Commented Nov 13, 2009 at 23:39
  • Hm... That's odd. Can you post a screenshot of the routing debugger output= Commented Nov 13, 2009 at 23:39
  • I am running this on the built in web server provided by Visual Studio 2008. Commented Nov 14, 2009 at 2:07
  • same result for /Content and /Content/ Commented Nov 14, 2009 at 2:16

2 Answers 2

7

It's a shot in the dark, but do you have a directory named /Content/ as well?

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

2 Comments

Bingo! Turns out the MVC template creates a directory named Content where it puts the Site.css file. Now i feel like an idiot :( Thanks so much for everyone's help!
Same here. But my code created a folder with the same name of a controller.
1

/Content is a controller, which is basically just a collection of actions. ASP.NET MVC needs to know WHICH action you want to run, so by leaving out the action asp.net mvc doesn't know what action to return and gives a 404.

You can tell it a default either by adding a route:

eg:

routes.MapRoute("ContentDefault", "Content", new {controller = "Content", action = "Index"});

The attributes are defined as follows:

'ContentDefault`: Name of the Route (must be unique in your routing table)

Content: The URL segment (try changing this to 'Content/Much/Longer/URL' and then go to http://localhost/Content/Much/Longer/URL to see how this works)

new {controller=.., action=...}: which controller/action combo to run for this route.

You could also override HandleUnknownAction in your controller:

    protected override void HandleUnknownAction(string actionName)
    {
         return RedirectToAction("index");
    }

Oh and incidentally, an extra piece of advice about routing.... if you add something to the route in braces { } these will be passed to the action as an attribute.

e.g. /Content/Much/Longer/Url/{page}

so the URL http://localhost/Content/Much/Longer/Url/999

will pass the 999 into your action, as the page attribute

public ActionResult Index(int Page) { }

I love MVC - never going back to WebForms - this is how web development should be!

1 Comment

Thanks for the info, this is very specific to what I am working on.

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.