In MVC3, how do I override the TempDataProvider globally? - asp.net-mvc-3

I'd like to change the TempDataProvider in an ASP.NET MVC3 application... I know I can do this on each controller by overriding CreateTempDataProvider... but I was wondering if there is a way to do this in 1 spot ("Global.asax?") for all controllers.
My reason is that my site is on a cloud server... and I want to implement the Post-Redirect-Get pattern in some cases, but I don't want the user to be sent to another server and never get his message.

It seems you could write your own ControllerFactory. Here you could then, after retrieving the controller from the base DefaultControllerFactory class, set the TempDataProvider to your implementation. See more details here.
This probably does what you need, but personally I would prefer more then next approach:
I find it a good practice to have all your controllers inherit from some 'base' controller class. Common controller logic (like overriding CreateTempDataProvider can then be done in 1 place.

Related

Where Do I Create External API functions or Classes? Proper MVC Structure for Web Frameworks

I am looking to add multiple API's to my senior project on an MVC based framework (Laravel). I understand the basic concept of MVC, but want to make sure that I am doing things according to best practice.
Basically, I am going to have a class/function that takes a query and calls that query on a Amazon's Product API. I have seen an example of calling API's from directly within the Controller on Laravel (see http://www.phplab.info/categories/laravel/consume-external-api-from-laravel-5-using-guzzle-http-client).
Perhaps I don't understand MVC well enough. Should an external API call be in it's own class? And if so, should it be a Controller Class or a Model Class? I hope the Stack Overflow gurus can enlighten me. Let me know if I need to clarify anything!
It depends to what you want to process with external API.
If it's a part of the business, it can be in Model (lot of people put
the business inside the model to follow the encapsulation principle
of OOP).
If it's the explicit process, it should be in Controller
(like most people do).
For example, if you have a model Transaction in bank transfer (that automatically convert the currency, it needs the external API to get the exchange rate), the external API call should be wrapped in model. So controller cannot modify the Transaction object and it will be safe.
In another hand, you can call to external API in controller, do some extra stuffs then set it back to Transaction object. It's also good because model always contains only properties. It makes application also clear enough.
They are 2 ways of use, none is absolutely right or wrong. But if you choose one, follow it, don't mix.
Another, both 2 are only ok. The better way is putting the external calls to other places (modules etc), then call it by single line in model or controller.

Is it possible to extend Yii URL routes and rules through a Controller?

Is it possible to extend Yii URL routes and rules through a Controller on the fly, or by extending the CController class itself?
I am really stuck with this, have not tried anything.
How to do this in/with Yii?
First you should be aware of what URL rules are used for:
Parsing an incoming URL. The urlManager component resolves a URL to a route so that Yii can call the right action in a specific controller and module.
Creating URLs from calls to createUrl() in your code.
Considering (1) above it is obvious, that you can not add URL rules in your controller if you want to use these URLs in your application. It's much too late, as Yii already has gone through the process of resolving a request to a route. Even if you'd only wanted to create URLs it doesn't make much sense as your application will never understand them.
The correct way to bring more dynamics into your URL parsing/creation is to use a custom URL rule class. In there you can write any code you want to create and parse even the most complex URLs. The topic is described in the manual.

WebAPI - Controller vs ApiController?

So this is kind of a semantic question I suppose. But in my WebAPI project, I have a view that is supposed to allow a user to find resources (it doesnt matter what the resources are). So I created a controller called FindResourceController which is derived from Controller. I then create my view using razor and jQuery. And my controller has a method, called Index which returns this view.
All good so far. But I ALSO want a ApiController to handle jQuery requests from my FindResource view. By convention out of the box, API urls are routed like api/FindResource/, so I should name my api controller FindResourceController and derive it from ApiController. But I already have this class. So then I could name it FindResourceApiControler, but the url maps to api/FindResourceApi/ which seems redundant. This just seems kind of kludgy to me. So either I'm missing something, or this is just the way it is... so, when doing something like this what do you do in your web api project?
Edit: So far, this is all I have found
Link to Article
Is putting the types in a different namespace an option for you? You could have a Controllers namespace and an ApiControllers namespace and have a FindResourceController in each. It might cause some conflicts though so it's not a perfect solution, but it might work for you.

Is there a way to check if an action is used in ASP.NET MVC?

I'd like to know if there is a way to find defunct action methods on controllers. I have R# and ran analysis, but it didn't seem to check if the asp code called an action. Is there anything that does?
Implement a global action filter that records the action name in a persistent store somewhere. This way you can track which actions do get executed and figure out what's missing from possible actions. It's a bit tedious but may work for your purposes.
No, a tool what not know what actions are required, as they are invoked by the routing configuration. I suppose you could write a tool which could check which actions are accessible given the current routing configuration, but then it wouldn't be able to know if those methods wouldn't potentially be used by other code, as they are marked as public.

KohanaPHP Controller. Should I group them?

I'm new to OOPHP and frameworks at all.
I'm just wondering...
I have few controllers:
dashboard
signup
login and few more
I've put them into users directory. Everything is working correctly, I'm just wondering if I should put everything in one controller and signup, etc. should be a method of users controller? Or am I doing it correct way?
Regards,
M
It's totally up to you. The stuff you currently have could probably all go into one controller (user controller in this case), but it can build up to request the separation you already have, e.g. separate controller for each action, grouped by a prefix.
Good thing about kohana is that it allows you to do stuff like this the way you want to, there isn't a single guideline about putting many 'common' actions into the same controller; do it as you like / find appropriate.

Resources