简体   繁体   中英

Asp.net MVC same view name, different paths

I want to have multiple path folder structure that contain same name views:

/profile.aspx
/admin/profile.aspx
/user/editpost.aspx
/admin/editpost.aspx

/Controllers
  |- PostController.cs
  |- ProfileController.cs

I want to be able to have all the regular pages in a folder and the admin pages in another folder. Do I need to organize my Views folder like:

/Views
  /User
    /Story
      |- editpost.aspx
    /Profile
      |- profile.aspx
  /Admin
    /Story
      |_ editpost.aspx
    /Web
      |- profile.aspx

or is there a way I can do this:

/User
  /Views
    /Story
      |- editpost.aspx
    /Profile
      |- profile.aspx
/Admin
  /Views
    /Story
      |_ editpost.aspx
    /Web
      |- profile.aspx

Also, how do I code/organize/use separate controllers for /User and /Admin that potentially have the same name?

Let me know if I have been unclear.

Thanks!

No problem. You can organise your folders in any way you choose. You can specify a view by name or even by its path in the Action method:

return View("~/Views/Posts/Index.aspx");

You should read this post by Phil Haack .

Basically, you're gonna have to create your own ViewEngine to work with your folder design.

One of the major problems with the first release (And all the RC and Beta's of course) is that ASP.NET MVC does not support areas. Areas are something that alternative MVC frameworks for ASP.NET have supported for some time and when your project gets to a reasonable size you're going to end up with possibly hundreds of controllers (all with unique names) in the same folder and your code is going to be very hard to sort through.

Your idea makes perfect sense and I hope that future instances of the ASP.NET MVC framework supports areas out of the box (so to speak). In the mean time it's easy to create your own Areas framework on top of ASP.NET MVC.

Here are some posts that will help you out:

Hopefully they're helpful to you.

There's another option too:
you might want to create custom ViewEngine and specify view/partialview locations.

For example:

 //Global.asax
 public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            ...                
            ViewEngines.Engines.Add(new ViewEngine());
            ...
        }
    }

public class ViewEngine : WebFormViewEngine
    {
        public ViewEngine()
        {
            PartialViewLocationFormats = PartialViewLocationFormats
                .Union(new[] {"~/Views/{1}/Partial/{0}.ascx"});
        }
    }

allows you to use 'Partial' folder for partial views.

But i would personally prefer Areas. Seems that's exactly what you need.

More than likely each edit view is going to require different fields so sharing editpost is not really viable.

If however you are editing [exactly] the same fields then perhaps organise the views as you have them and then render a partialview which you can pass a model to.

The partialView can be in a common place which keeps your seperation of concerns as far as the views are concerned as well as code reuse with the partialview.

Each controller then has its own name such as UserController and AdminController. Inside each of these you have your editpost action no probs.

Does this help or do you need more?

Did you try passing the specific view in the Controllers? I think you could bypass MVC common Views folder design by passing the specific view on any controller (However I will not recommend it).

I am not 100% sure but I think you could do things like


return View("User/Story");  //or something in that matter

But then you will need to change your routes, so the controllers get routed accordingly. I think I read it some time ago, but I could not found the reference. Let me know if it works.

However, if your Views are dictating your design, then maybe you should not use MVC after all.

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