开发者

Newbie MVC Routing

开发者 https://www.devze.com 2022-12-12 15:33 出处:网络
I\'m clearly missing the concept of routing - for an experiment I\'ve set the route as routes.IgnoreRoute(\"{resource}.axd/{*pathInfo}\");

I'm clearly missing the concept of routing - for an experiment I've set the route as

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Standard",
                        "{devicetype}/{devicesub}/{language}/{culture}/{controller}/{action}/{id}",
                        new
                        {
                            devicetype = "pc",
                            devicesub = "def",
                            开发者_运维问答language = "en",
                            culture = "int",
                            controller = "Home",
                            action = "Index",
                            id = ""
                        }
                        );

My index page is in Views/pc/def/en/int/Home

When I run it I get an error searching for /Home/Index.aspx

It seems to still use the default structure and not my more complex one - what am I not understanding?


The way the content of the site is stored does not reflect the route but is defined by the Controllers and the Views so although your route is complex you're still ending up at the home controller so MVC is going to be looking in /views/home for the appropriate view which in this case is index.

One of the hardest things I've found to get my head around is the separation of URL from the processing and more importantly content - its right and its clever but the fact that routing and result can be radically different (in terms of finding things in your directory structure) is, erm, interesting (-:

As a practical experiment, do nothing other than relocate your index page to /views/home/index.aspx and see if that resolves the problem...


I'm a bit of a newbie myself so this might not be correct, but as far as I know, the path of your views are always located in the "Controller/Action" path. The additional properties you have specified are simply just querystring values being submitted additionally with the request.

Hope it helps...

G


So, the relevant lines if your controller simply ends in return View() or return View(modelData) are:

controller = "Home",
action = "Index",

All URLs matching your above route will land there, unless your URL is for something like /pc/def/en/int/Widgets, in which case you will route to WidgetsController/index.

It sounds like you want to have different views for the same action. If you want to have different views depending on the parameters passed to your controller, you can do that. You need to be explicit about it when you return your ViewResult. You can return View("SpecialView",model) and the view engine will look for SpecialView.aspx in your controller's view directory. Of course, "SpecialView" could be replaced with an appropriate string for your app, and could be generated programmatically if it makes sense.


Many thanks for the input folks - i think i'm begining to understand

It does indeed work if i place the form in views/home - however most commercial sites are much more complex than 2 levels

As you have probably gathered from the structure what i was trying to experiment with was different forms for device type (pc, phone, mobile) and culture but using a single controller as the business logic is the same regardless of the style and language of the presentation


Just for further info

I've changed the directory structure to Views/Home/pc/def/int and auto generated the path as Jason suggests and this works fine - I had to change the structure as Views/Home seems to get added to the front of the search regardless of the string you supply in the View command

0

精彩评论

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

关注公众号