开发者

What am i doing wrong with this in asp.net-mvc?

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}");
开发者_如何学C
            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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜