简体   繁体   中英

C# asp.net mvc 1.0 routing

In my web app. i want to setup a route like this:

/document/quality/index
/document/general/index

/document/quality/detail/[id]
/document/general/detail/[id]

As you can see i have two kind of documents: general and quality. What is the best way to set this up in my global.asax file? I tried the following, but i don't get it work:

routes.MapRoute(
    "QualityDocument",
    "Document/Quality/{action}/{id}",
    new { controller = "Document", action="Index", id= ""}
    );

routes.MapRoute(
    "GeneralDocument",
    "Document/General/{action}/{id}",
    new { controller = "Document", action = "Index", id = "" }
    );

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

EDIT

I have it working right now, but when i change the action="" in my asax file it doesn't work anymore:

Global.asax:

routes.MapRoute(
    "GeneralDocument",
    "Document/General/{action}/{id}",
    new { controller = "Document", action = "blaat", id = "" }
    );

DocumentController:

public void blaat(int? id)
{
    Response.Write("algemeen");
    // return View();
}

Now i get the Resource not found error. But when I use index instead of blaat it is working. How come?

Perhaps add in the controller attribute, but constrain it to be the document controller.

routes.MapRoute(
    "QualityDocument",
    "{controller}/Quality/{action}/{id}",
     new { controller = "Document", action="Index", id= ""},
     new { controller = "Document" }
);
routes.MapRoute(
    "GeneralDocument",
    "{controller}/General/{action}/{id}",
    new { controller = "Document", action = "Index", id = "" },
    new { controller = "Document" }   );

you should be able to just prefix the route with "Document" and then set the defaults like:

 routes.MapRoute("DocumentView", 
                "Document/{controller}/{action}/{id}", 
                new { controller = "General", action="Index", id = ""  }
            );

remember with routing you want to be completely defining! when setting out the route. dont let those unexpected routes through :D

Check out Scott Hanselman presentation at MIX very funny and your pick up some great tips!

http://www.hanselman.com/blog/Mix09FirstHalfRollupAndSessionVideos.aspx

Try this:

routes.MapRoute(
    "QualityDocument",
    "Document/Quality/index",
    new { controller = "Document", action="Index" }
);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM