|
why would you have different controllers for each action? unless each of the above action does exactly the same thing, in which case have one controller for all the similar actions
ie one controller to do all the saves, one to do all the deletes etc
but normally you have different information to save etc on each view, so group all the actions for that view in one controller, even if you have a single class that actually does the save it can be called from multiple controllers
you can even have multiple views using the same controller, so all your customer views go to one controller, all your sales views to another etc
as for your example you are not actually writing the same code what you are doing is using an inbuilt functionality to control flow, you don't seem to have a problem with using multiple @html.helpers functions, what you have I n
protected void ActionResult Index()
{
return View();
}
is a server side helper using its default functionality, that you are not using its full capacity does not make it invalid.
the system is called Model - View - Controller and is designed to pass data to the html by use of a model, have that data changed by the UI and then the Model is passed back to the controller for processing, that you are using it for what sounds like a different process does not make the concept invalid
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: why would you have different controllers for each action? unless each of the above action does exactly the same thing, in which case have one controller for all the similar actions
You can group actions together for a view, but it is still one view controller for each view, isn't it?
Bergholt Stuttley Johnson wrote: that you are not using its full capacity does not make it invalid
I think that perhaps you're missing my point? Let me give a clearer example:
I have an application with a header, nav bar and a footer. I use a MVC layout file to bring the basic structure of my application to life. So far so good.. So I create a number of pages which look up data and present it to the user. The need for the controller code that I write is clear here - I'm performing some logic.
However, I have a set of pages - let's call them "help" pages, that do no database lookups and have no real logic. I still want to keep my header, nav bar and footer (with content appropriate to the logged in user, perhaps)..
In standard ASP.NET MVC I still have to add a view controller for each help page (view). I need to write a C# action. Whenever I add a new page (view) or rename pages (views) I need to update my C# code and recompile and publish the entire application.
How does that make sense? It's not a case of under-using the framework, it's day-to-day development. Some pages (views) need their own logic, others don't - the point is that with ASP.NET MVC, I have to write logic for every page (view) whether it's needed or not.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I can see what you are getting at I think, firstly still not sure why you need a new controller for each view? if its only initialization then you can put them all in one controller, unless you are calling the views the same name!
whist I can see that three lines of code repeated numerous times may seem like a silly overhead, these three lines are initialising the MVC process, think of it as similar to initialising a variable, you do that hundreds of times too but accept that as part of the process.
as for updating well that is how things work, if you object to that then don't use MVC if you want the flexibility of MVC then you accept that its compiled and will need recompiling when you change code, if you don't want that then use a scripting language and HTML
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: I can see what you are getting at I think, firstly still not sure why you need a new controller for each view? if its only initialization then you can put them all in one controller, unless you are calling the views the same name!
Not quite, as it's also relevant to actions within controllers. An example:
- Views
- Home
- Index.cshtml (pretty much static content)
- UsagePolicy.cshtml (pretty much static content)
- Customers
- Index.cshtml (pretty much static content)
- List.cshtml (a list of customers)
- Edit.cshtml (the "edit customer" form)
- Shared
With ASP.NET MVC as it currently stands, I need the following controllers and actions:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult UsagePolicy()
{
return View();
}
}
public class CustomerController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult List()
{
..Customer list logic
}
[HttpGet]
public ActionResult Edit()
{
..Form initialization logic
}
[HttpPost]
public ActionResult Edit()
{
..Form relevant logic
}
}
What I'm suggesting reduces the code down to this (HomeController is no longer needed):
public class CustomerController : Controller
{
public ActionResult List()
{
..Customer list logic
}
[HttpGet]
public ActionResult Edit()
{
..Form initialization logic
}
[HttpPost]
public ActionResult Edit()
{
..Form relevant logic
}
}
Surely reducing the amount of code down in any project makes sense more than writing the same code in different classes over and over again?
Bergholt Stuttley Johnson wrote: hink of it as similar to initialising a variable, you do that hundreds of times too but accept that as part of the process.
Not quite (again). I only add a variable where I have a use for it - it's optional. Actions (and controllers) are mandatory whether or not I have any logical code to add.
Common methods would normally be placed within a base class, or you might decide to add in a route, but that still means recompiling and re-publishing the entire application every time someone adds a new view/action name.
What I'm getting at is this: why not (in ASP.NET MVC) make actions (and controllers) optional, in that I only write them if I actually need to include some functional code? The default behaviour should be (in my opinion) that if a route is requested that goes through to a view without an action and/or controller, then it simply returns the view.
Bergholt Stuttley Johnson wrote: f you object to that then don't use MVC
I love ASp.NET MVC, but even the MVC guys themselves work on improving it. If we just accept things as they are, we'll never move forward
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
so what you are saying is that it should create the controller/action code automatically if it isn't in your code.
ok but this allows the runtime exception if the view doesn't exist when the existing method will not compile if the view is missing
whilst it is a possible enhancement it does break the idea of the model-controller-view process, and whilst your example does show it may be a nice to have it is not the perceived flow and may have little traction in the future development of MVC, it will depend on exactly how much this is seen as an advantage over other enhancements
if you love it then you must except that their are something that are inherent to the way the architecture is and if you want this to change then perhaps MVC.net may not be the platform for you, MVC itself is not restricted to .net and there are flavours that are script code based and does not require compiling, however they don't have the razor engine
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: so what you are saying is that it should create the controller/action code automatically if it isn't in your code.
No, that isn't what I'm saying. Perhaps it's easier if you take a look at the example project here..
Using ASP.NET MVC Without Controllers or Actions for Each View[^]
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I don't know MVC real well, but isn't there a way to create a "base" controller with
protected void ActionResult Index()
{
return View();
}
in it and use that as default for all views, and the use specialized controllers when needed?
If it's not broken, fix it until it is
|
|
|
|
|
But what about when you have another view called "About" for example with static text? You now need to add the "About" action to one or more controllers, recompile, republish..
Even to add a single static HTML page I'd still need to create a new view controller - that doesn't seem logical to me..
And you need to know up-front what the names of your views will be.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Brent Jenkins wrote: Even to add a single static HTML page I'd still need to create a new view controller
No you wouldn't. Just add the page (or, more ideally, the folder it's in) to an ignore route and let IIS serve the page natively.
|
|
|
|
|
And if I still want to use the shared layout or Razor?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Then it's not an html page anymore.
|
|
|
|
|
So I have a MVC layout with a header, nav bar and footer and the content contains just this:
<p>Hello world</p>
In the world of standard ASP.NET MVC, I still have to create a view controller with an action to return my view.
That's what I'm talking about - the content of the view is static but I still need to code, recompile and publish.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I've never dynamically deployed cshtml, but I believe it's compiled on the fly.
Couldn't you create a catch-all route that passes the viewname as a parameter instead of the action name?
routes.MapRoute(
"my-route",
"{controller}/{viewname}",
new { action = "GetView" });
|
|
|
|
|
Surely better to just re-route views without an action (or controller) through to a default controller? For anything else, leave the "standard" behaviour.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
you could but why?
surely that the views are different, if they are purely html and not requiring a model you could just have a case statement and a redirect to view
I can see that having an action for every view is extra code but if you have so little in a controller maybe you should be thinking is MVC the right technology!
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
you don't need to create a new controller if you don't wish, you could just use one controller for everything (yeuk) and use route mapping to wire it together
not sure I'd like to have to debug/fix it though
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: not sure I'd like to have to debug/fix it though
Is it any worse than fixing a hundred view controllers with the same code repeated a hundred times?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
yes as that would involve using a 3 foot wooden debugger to the responsible developer's kernel
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Doesn't every view controller have a default/initial action?
You could have a base class for view controllers I guess but then you're still tied into having a common default action name across the board.
That's where I came up with the idea of having a default controller that handles any routes that don't have the appropriate action and controller set up. When 99% of your calls go into WebAPI, view controllers are almost obsolete
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
nope, you can use routes to define how this is organised, however I wouldn't recommend doing this
kinda misses the point of MVC
Routes are a very powerful bit of MVC and are underused
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Routes are powerful, but why do I need to recompile my entire application and publish the whole lot, just because I've changed the name of a view?
Even worse, if I add a static page I still need to add (under out-of-the-box ASP.NET MVC) a view controller, then re-compile and publish the entire application..
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
why , if its a static page would you need it to be a view? html pages still work just fine
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
And if I still want to use Razor there, e.g. @Html.Partial(...) ?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I would have to see how its organised to work that out, it does seem to against best practices
I find I have very few actions that do nothing but return the view
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
If you're creating AJAXified apps or SPA's then you'll probably have those kind of actions in every controller
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|