I have a controller file "global", which contains function actionUpload. And I need to create an AJAX endpoint to use this action:
/protected/components/global.php
Yii::app()->createUrl("Path to actionUpload");
How could I specify the path to actionUpload?
You can't create a link to a component. You can wrap your component in a controller action, but it is impossible to access the component direct.
I assume that this global class extends CController otherwise this wouldn't be possible. So then you need to add a controllerMap to you configuration. See http://www.yiiframework.com/doc/api/1.1/CWebApplication/#controllerMap-detail
After that you can create url like Yii::app()->createAbsoluteUrl('controller/upload'), if controller is the name of the controller registered in the above function.
Related
Consider a fresh Asp.Net Core 2.1 MVC Web app created via the Visual Studio 2017 template. Now, consider a custom view (MyView) and also a controller (ActualController) so that the project structure looks similar to this picture:
The MyView shows nothing special, and it's off the point. However, the page should appear when the user enters an URL like http://(domain)/desired/myview or also via a hyperlink in the home page:
<a asp-area="" asp-controller="Desired" asp-action="MyView">MyView</a>
Now let's focus on the controller, which is a class named differently from what the routing expects:
[Route("desired")]
public class ActualController : Controller
{
[Route("MyView")] //without this the method won't be called
public IActionResult MyView()
{
return this.View();
}
}
From what I know, by decorating the controller with a Route attribute tells the URL resolver to match this class. However, the mapping works only if I explicitly add a (redundant) Route attribute on the target method/action. If I remove it, the path won't be found, and the server returns a 404-error.
The question is: why should be mandatory to decorate with Route the method, even the action is implicitly defined by the method name (as usual)?
NOTE: is rather simple for me to rename the controller class, but I'd like to know what are the reasons behind this behavior.
You are overriding the default route of [controller]/[action] with [Route("desired")]. Since you don't define an action parameter on controller level, all other routes have to be done explicitly.
Changing the top route parameter to [Route("desired/[action]")] should solve it and the method name will be used as parameter. You can still override single actions if you want to name them differently by adding the [Route("")] attribute to them.
Also see the docs (Token replacement in route templates) for further description on the route parameters
I am working on MVC and i started learning Umbraco, I didn't get how to bind the umbraco page with mvc controller get method to show the database values. can anyone suggest any url or video?
Thansk...
What you're looking for is Umbraco route hijacking.
You can read about it here.
https://our.umbraco.org/documentation/reference/routing/custom-controllers
It's easiest to demonstrate with an example : let's say you have a Document Type called 'Home'. You can create a custom locally declared controller in your MVC web project called 'HomeController' and ensure that it inherits from Umbraco.Web.Mvc.RenderMvcController and now all pages that are of document type 'Home' will be routed through your custom controller! Pretty easy right :-) OK so let's see how we can extend this concept. In order for you to run some code in your controller you'll need to override the Index Action.
So, basically, you "simply" need to create a controller named after your document type, so for example, a document type with the name "TextPage" would need a controller called "TextPageController". Now, if you read through the documentation, you'll find that your "TextPageController" will need to inherit from the RenderMvcController. Here's an example how to achieve this.
public class TextPageController : RenderMvcController
{
public ActionResult Index()
{
return View("~/Views/TextPage.cshtml");
}
}
This forum link may help you:
https://our.umbraco.org/forum/developers/razor/38242-Umbraco-MVC4111-Surface-controller-using-an-AJAX-form
I want to call a method in the controller. To do this, assuming default routing, I have to have a view that matches the controller I'm calling.
So if I have a contoller action
public ActionResult Edit(booking booking)
then I must have a view called Edit.
Is that right?
What I want to do is call any action in my controller from a given page without there being a view of the same name.
So if I'm on the Edit page, I should be able to call an action named createproduct, without there being a createproduct view.
You don't need to have a view for every controller action. You can just call an action by calling the correct URL, that's one of the advantages of MVC and routing. Routes don't have to represent a physical location. You should be able to do the following /Controller/Edit/booking
A controller action doesn't have to return a View. You tend to see the action method return View(), which renders a view with the same name as the action method by default. But you can return View("SomeOtherViewName"). You can also return various other results, such as FileResult (returns a file), HttpStatusCodeResult (returns an HTTP response code with no content), JsonResult (returns Json), Content (returns some string) etc. The Controller class has methods to help return some of these result types: File, Json, Content.
I have a method in my controller that I don't want to be called from the address bar in the browser...
Is there a way to do that? Maybe some kind of annotation, modification in the route configuration? Which are my options?
If you are going to use this action only from within your controller or Views then you can use ChildActionOnly attribute.
If you want to access it using POST then you can use [HttpPost] attribute.
But if you wish to use it using GET (i.e. using AJAX call etc) and don't want users to access it using address bar then you can follow this tutorial to make your actions AJAX only.
Or, if you simply want a method that is not an Action at all (i.e. cannot be called using HTTP) then you can either make it private or use [NonAction] attribute
Use NonAction attribute on the method.
I may be crazy, but I think that I one time saw that you can do the following:
create file: app/controllers/hello_controller.rb
create file: app/view/hello/foo.html.erb
without having to create a change in the routes.rb and a method in hello_controller.rb, I thought that the default mapping of url:
/hello/foo
would output the foo.html.erb because the '/hello' would know to use the default hello_controller, and the '/foo' would know to route to action 'foo' and thus map to the view hello/foo.html.erb???
Basically, I am creating some quick static pages and have to put in 4 different changes: controller, action method, routes, and view....is there any way to do this quickly and avoid all the process and just get rails to pick up 'default' controller and view?
In your example, you would do rails generate controller hello foo. This would create a controller called hello with a method called foo.