|
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.
|
|
|
|
|
Not sure if I was using ajax in that way I would be using MVC at all, just use cshmtl pages in a asp.net app
sounds like you have found a way of using MVC that it was not design to do but in a way that works, no problem with that, however I do find it a bit harsh to condemn the technology just because you are using it differently to they way it was originally designed to work
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
I'm not condemning it - it does at least provide a mechanism whereby you can "fix" this issue.
Bergholt Stuttley Johnson wrote: sounds like you have found a way of using MVC that it was not design to do but in a way that works
Just using plain old ASP.NET MVC with Microsoft WebAPI, TypeScript and KnockoutJS.. pretty much standard fare these days
..and I usually plug this in to make actions/controllers optional unless I actually need to write specific code for them..
https://github.com/brentj73/ControllerLess[^]
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.
|
|
|
|
|
SPA's are to MVC as WPF is to Windows Forms.
Can you combine the two in a single application? Sure. If you want to have lots of headaches and duplicated code.
It's much simpler to do one or the other. In my SPA's I have exactly one web page: index.html, and zero page controllers. Everything is webapi and javascript. Then I'm not trying to do mental gymnastics around partial pages vs. partial views vs. shared views, or api controllers vs. page controllers (routing gets to be a mess when you go down this road).
|
|
|
|
|
Vark111 wrote: Sure. If you want to have lots of headaches and duplicated code.
Hey, that's my point Standard ASP.NET MVC means you end up with code in WebAPI and in the view controller for the same view.
I'd disagree about the headaches though - a single page app may only have one URL to a view, but depending how it's configured it could well be returning partial views as a HTML response to an AJAX call, as opposed to JSON encoded data (I've developed systems that do both). You can get just as many issues using ASP.NET MVC and Razor without AJAX or WebAPI once an application gets complex enough.
A SPA with only a few different HTML elements can be done without using partial views, but if you're working on a large-scale application it's just not practical to try and load every possible combination of HTML elements the first time you hit the page. One system I recently worked on (not my design) had over 6,000 views/partial views.
I really hate duplicated or unnecessary code, that's why I'm talking here about actually removing actions from view controllers that only return a view leaving only code that actually performs a logical function (or even better, removing the controller completely where only static content is contained in a 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.
|
|
|
|