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.
Related
In Laravel, I would like to know how to make ajax call internally without using the the route system. I'm not sure what other ways I can access internal methods, without routes.
The reason I'm looking for this is because the way the application is setup, routes always return html, even if a JSON response, because the framework is loaded inside the content area of a parent website (using different framework) which always had a header and footer(ugh, I know). I have another post about this, but lets please stick to the question.
I just need this framework to talk to itself, from the $.ajax() request, straight to the controller, bypassing the route, so maybe a JSONP, or some way to access methods directly without going overboard.
I know I did this sort of thing using FOSJsRouting from Symfony in a previous app to accomplish this sort of thing, but I had to make a wrapper to access the methods. I don't want that kind of elaborate setup if possible here.
Are there other ways?
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.
I'm a Laravel 3.x beginner with a CI background.
I'm very acquainted to use controllers rather than routes and I'm having issues trying to use controllers in Laravel.
For example: let's say I have the home_controller and the "about" action. My problem is that I'm only able to access the "about" action by setting a route that points to it - something I think is undesirable.
Is there a way to get the "about" action to work without setting a route?
In laravel, everything can be accomplished using either Routes and/or controllers.
However, using both routes AND controllers is suggested for great flexibility. See this article for more informations and some examples of how to combine routes with controllers.
Anyway, if you want to use controllers (which is perfectly acceptable), you need to register them in your routes.php with Route::controller('yourcontroller') before you can use them.
Everything has to be routed in Laravel. But, you don't have to manually route each method. You could do something along the lines of Route::controller('admin').
See here: http://laravel.com/docs/routing#controller-routing
I like Mike Anthony solution. When you're using only controllers this detect method is everything you have to do - this will register automatically all of your controllers. Best hand free solution so far.
The usual controller registration is, as the guys already mentioned, this:
Route::controller('controllername');
You have to register all controllers like in the example above. It is one line of code per a controller, and it is the rule.
But if you have a static page or a login action (page), a good practice is to create a Route controller (anonymus function), not a classic controller (in controllers folder).
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.
I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ).
I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question.
However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext. This WCF service is contained within the same project as my MVC project so has access to all my models etc.
I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. I thought there was something in mvccontrib but it doesn't seem to be there anymore.
The closest I've found is the accepted answer to the aforementioned question, but it unfortunately breaks with RenderPartial within the view you're rendering.
I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now?
BuildStarted and I have put together a standalone Razor templating engine which you might find useful (Github). You'll need to adapt your .ascx content into a simple string template, but it should do the job.
If you've got NuGet, you can run Install-Package RazorEngine
You may checkout the following blog post. Also Postal is worth looking at.
You need to create a fake HttpContextBase with a fake HttpRequestBase that return meaningful values from their properties.