I want to trigger an action for an intent once 2 types of Cereal have been requested but it seems an action can only have one parameter instance of a given type?
Am I misunderstanding the purpose of actions?
Related
I have an action where I am supposed to capture many primitive values including Enum types. I have created a model to match the required values.Can action parameter be a model in ASP.NET WEB API? Or I need to create a view model?
You don't necessarily need to create a view model.
Models, view models, DTOs can all just be classes with properties on.
How each can or cannot be used is a decision you need to make.
Usually the main requirement from an API endpoint perspective is that the class into which the parameters are being deserialised has a default constructor. Then the parameters from the request will be assigned to properties of the equivalent name.
I would like to know the differences between a custom action filter and a custom action selector in ASP.NET MVC.
Say we want to restrict who can have access to an action method on a controller based on some rules. I could either create an action filter extending the ActionFilterAttribute class or extending the ActionMethodSelectionAttribute class, so that I could have something like:
[MyRestriction]
public ActionResult AnyAction(){}
Could anyone explain the differences between them so that I can make the right decision?
If you look at the documentation for ActionMethodSelectionAttribute, you will see at the very bottom of the page that there are a number of other classes that derive from this attribute.
These include:
Microsoft.Web.Mvc.AjaxOnlyAttribute
System.Web.Mvc.AcceptVerbsAttribute
System.Web.Mvc.HttpDeleteAttribute
System.Web.Mvc.HttpGetAttribute
System.Web.Mvc.HttpHeadAttribute
System.Web.Mvc.HttpOptionsAttribute
System.Web.Mvc.HttpPatchAttribute
System.Web.Mvc.HttpPostAttribute
System.Web.Mvc.HttpPutAttribute
System.Web.Mvc.NonActionAttribute
In other words, these are the attributes which control which Action Method is selected during routing when there are several different choices to choose from (ie there are 2 different Index methods, one decorated with [HttpGet] and one with [HttpPost]).
ActionFilterAttribute, on the other hand, is called only when an action is actually executing.
Think about it this way, The selection can run even if the action doesn't execute, the ActionFilter only runs if it does. The selection filter is only used to determine whether the action is a match condition, the action filter is used to do some action before, after, etc.. the action or response is executed.
I'm currently using MVC5.
Imagine the scenario where one controller ActionA does its work and the redirects to another controller ActionB but also wants this second method to display a message on its related view.
If Controller ActionA sets the ViewBag.Message and then calls RedirectToAction, when ActionB starts, the value of that Message is gone.
What's the best way to pass a message from one action controller to another, without using Session ??
You can use TempData:
Action A:
TempData["Message"] = "Hi";
Action B:
var message = TempData["Message"];
Once you call the getter in Action B, the information will be automatically removed from memory.
This article is a really good explanation of the various persistence techniques available in ASP.NET MVC.
You say no session, but TempData can be a good place depending on what you're passing. It's essentially Session except that the item will be removed from Session once you've accessed it. TempData actually uses session by default unless you write your own provider.
Passing it as an option in the query string, or passing it as a parameter in the routing (similar to what AJ says)
Other than that you've got the standard run of the mill options that are provided in ASP.NET. HttpContext.Items or maybe HttpContext.Cache. But both of those are shared across the entire application domain so management can get tricky.
Remember, web is supposed to be stateless. So if it's really important for that message to get there, you probably want to put it in the URL somehow (query string or routing), or use a database.
I would put an optional argument on the method signature for ActionB, for example:
public ActionResult ActionB(string message = "")
Note that optional parameters need to be last in your parameters list.
I am looking at a way to access parameter passed from different Url to use in my web Flow. I am getting URl like
----/newUserRegistration/userRegistration?execution=e20s1&tkn=f9e1cfe077c75ec79f39c61543407cac96ae57e2eca6576e5312b2a266cd8df0. New user registration is my controller name and userregistration is flow name. I have to capture tkn parameter and use this for my flow. How do I do this ? Please suggest me on this....
Given that your Webflow DSL is correct in an action block you can access it with params.tkn.
I'm sorry to ask such a basic question, but it's kind of fundamental for me. To better understand filters, I need to understand this notions. Although I'm on ASP.NET MVC for few months now and now are doing nice demos, I'm more familiar with Action method concept than action result.
What are:
Action Method?
Action Result?
How are they related?
Let's say I've this
public ViewResult ShowPerson(int id)
{
var friend = db.Persons.Where(p => P.PersonID == id).First();
return View(friend);
}
How those concepts apply to the above code?
Thanks for helping.
In your example ShowPerson is the action. Each action needs to return an action result (In your case it returns a view). So when a controller action method is invoked it does some processing and decides what view would be best adapted to represent the model.
There are many different action results that you might use. They all derive from ActionResult:
ViewResult - if you want to return a View
FileResult - if you want to download a file
JsonResult - if you want to serialize some model into JSON
ContentResult - if you want to return plain text
RedirectResult - if you want to redirect to some other action
HttpUnauthorizedResult - if you want to indicate that the user is not authorized to access this action
FooBarResult - a custom action result that you wrote
Answer by #Darin-dimitrov is very much upto the point. But I see explanation given on MSDN also very much helpful.
Action methods typically have a one-to-one mapping with user
interactions. Examples of user interactions include entering a URL
into the browser, clicking a link, and submitting a form. Each of
these user interactions causes a request to be sent to the server. In
each case, the URL of the request includes information that the MVC
framework uses to invoke an action method.
When a user enters a URL into the browser, the MVC application uses
routing rules that are defined in the Global.asax file to parse the
URL and to determine the path of the controller. The controller then
determines the appropriate action method to handle the request. By
default, the URL of a request is treated as a sub-path that includes
the controller name followed by the action name. For example, if a
user enters the URL http://contoso.com/MyWebSite/Products/Categories,
the sub-path is /Products/Categories. The default routing rule treats
"Products" as the prefix name of the controller, which must end with
"Controller" (such as ProductsController). It treats "Categories" as
the name of the action. Therefore, the routing rule invokes the
Categories method of the Products controller in order to process the
request. If the URL ends with /Products/Detail/5, the default routing
rule treats "Detail" as the name of the action, and the Detail method
of the Products controller is invoked to process the request. By
default, the value "5" in the URL will be passed to the Detail method
as a parameter.