views:

45

answers:

3

I gave this in my site.master

<li><%= Html.ActionLink("Material", "Index", "Material")%></li>

But my link doesnt seem to get my material controller Index method... I have this in my global asax file,

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
                "Default",                                              
                "{controller}/{action}/{id}",                            
                new { controller = "Material", action = "Index", id = "" }  
            );

        }

My controller:

public class MaterialController : Controller
{
    //
    // GET: /Material/
    Material material = new Material();
    public ActionResult Index()
    {
        var materials = material.FindAllMaterials();
        return View();
    }
 }

What am i doing wrong.... When i click the link i get The resource cannot be found. error.. Any suggestion...

A: 

What do you mean by "But my link doesn't seem to get my material controller"? What is the link it generates?

Download the routing debugger from here: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Register it and see what route it is using when you type in the url manually.

Raj Kaimal
@Raj ya i did type in the url manually it gave me the error `Resource not found`
Pandiya Chendur
A: 

Dump question -> make sure you've saved/compiled. What hints that u didn't do this, is that u have two routes with the same name (aka. Default). Compiler should error.

Secondly, try to manually goto that resource. Ie, goto http://localhost:/materials/index and see if that works. Of course, replace the localhost with whatever is the dev url of your site. If that works.. continue.

Thirdly, hover the mouse over the html anchor (aka. the a href) and see what is displayed in the browsers 'status' bar. It should list the resource url which that anchor will redirect you to. Does it say 'http://localhost:/materials/index' ??

basically, the code looks ok for the HTML.ActionLink .. but there's a few other possible things that have happened to screw this up.

Pure.Krome
A: 

You shouldn't map a route for that method actually. It's totally normal by convention. http://localhost/material or http://localhost/material/index would already be routed to your action method. Create a route if you have to make a customized URL. Just delete your own route and it should be fine

Ufuk Hacıoğulları