views:

23

answers:

1

Hi Guys,

Please find below my ActionResult Index:

public ActionResult Index(string SectionText)
{

    var products = from p in db.Products
                   where p.CategoryID == SectionText
                   //orderby gs.SortBy
                   select p;

    return View(products.ToList());
}

This is throwing the error below: -

Server Error in '/' Application.

The resource cannot be found.

Description: 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.

Requested URL: /Sections/daughterboards-894/

Any Ideas would be much appreciated. this is using the built in vs web server.

Thanks

A: 

Well you are requesting /Sections/daughterboards-894 and there's nothing that would relate this url to the Index action. If you are using the default routes your request should look like this: /sections/index/daughterboards-894

Of course this assumes that you have a route capable of handling this URL:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{SectionText}",
    new { 
        controller = "Home", 
        action = "Index", 
        SectionText = UrlParameter.Optional 
    }
);

If you want to have an url like /Sections/daughterboards-894/ you will need to set a special route for it:

routes.MapRoute(
    "MySpecialUrl",
    "Sections/{SectionText}",
    new { 
        controller = "Sections", 
        action = "Index", 
        SectionText = UrlParameter.Optional 
    }
);

Remark: As a side note I would recommend you abstracting your data access into a repository instead of directly accessing it in your controllers. This will make your code much easier to unit test.

Darin Dimitrov
Thanks i added the route but this didnt have the desired effect and im still getting the same error
ashga
Just an update this link here works but if i remove the index it doesnt. however Index is specifield as the default optionhttp://localhost:21063/Sections/Index/firewalls-hardware-832 - workshttp://localhost:21063/Sections/firewalls-hardware-832 - doesnt work...
ashga
Thanks I figured it out the new route works but i needed to put it before the default route school boy error!
ashga