I have been using openid selector to implement openid login on a web site following the tutorial here.
It all works ok but the openid_identifier is read in the controller from the request like so Request.Form["openid_identifier"] and I wanted to instead add the selected openid_identifier to the view model and have it set in there instead (so I can add other things to the view model or so I can test the controller more easily).
Can I do this? How?
Does changing
public ActionResult Authenticate(string returnUrl)
to
public ActionResult Authenticate(String returnUrl, String openid_identifier)
do it for you?
Related
I am new to mvc and I need to create an application to where the user will be using one of 2 urls. One url will direct to a generic registration and the other will direct to a customized user profile update page. (In a website this would be in the page_load method.) Where would be the correct place to write this logic within the mvc application?
In your WhateverController, you have to create 2 methods :
public ActionResult GenericRegistration()
and
public ActionResult ProfilPage(int profilID)
So you have 2 urls : /Whatever/GenericRegistration and Whatever/ProfilPage/{id}
Additionally, you can create another ProfilPage method who can accept the POST action from your html form when the user update its data :
[HttpPost]
public ActionResult ProfilPage(MyFormModel formModel)
{
// update user profile
}
I read here that I can't overload actions in MVC because of routing confusion
I tried to overload Index() in HomeController and I got the exception as article said, but I noticed that
microsoft has overloaded the actions in AccountController
public ActionResult Login(string returnUrl){}
public ActionResult Login(LoginModel model, string returnUrl){}
Please need clarification, thanks
Microsoft has overloaded this by setting HttpGet and HttpPost. One for GET request and another for POST request. What about your code?
[HttpGet]
public ActionResult Login(string returnUrl){}
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl){}
Till today you cannot overload your controller's Action method with same name but different parameters.
The only possibility is to have two overload, and for that you need to set the method's property to HttpGet and HttpPost. For example
[HttpGet]
public ActionResult foo(string myString)
{
return View();
}
[HttpPost]
public ActionResult foo(MyViewModelClass object)
{
return View();
}
And regarding your confusion,
From general convention, first method should be of type Get which gets called when someone sends request to access that page.
Second method is called when user submits a form with his login details.
In AccountController first method works with GET method, second with POST one. It was realized by attribute [HttpGet] and [HttpPost].
Read more about get and post here.
In addition to above answer we can add name attributes along with HTTPGET and HTTPPOST as
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(some parameters)
{
//code over here
}
After this we can call it as :- /MVC/EmployeeController/Edit/1
Some definitions first:
Overloading is a form of polymorphism, in particular an interface, in the sense of a class publicly visible part, related one.
When we speak about inheritance we mean overriding.
Action is a segment of a URL.
Back to your question...
It is the ControllerActionInvoker which is responsible for finding the method to which an action is mapped. Given GET and POST we see polymorphic methods in a class mapped to the same action, but serving different HTTP methods (any action, that is, URL segment polymorphism here?!). And again yes, we may use ActionNameAttribute (MVC v5) to map an action to a class method, but again, this has nothing to do with any sort of polymorphism. Simply put, all that happens is in the ControllerActionInvoker and has nothing to do with overloading or overriding, and finally with any sort of polymorphism -- it is just mapping.
Conclusion.
A simple question arises:
What in a string (a segment of a URL, 3.) relates to any one of the OOP definitions (1. and 2.) above? Or, in other words, can any of the OOP concepts above be transformed to a (part of a) string?
The fact that a transformation between URL segment, which happen to be called "action", and a class method exits does not imply that all we know about the first mechanically can be applied to the second and vice-verse. The question is misleading and its main intention is to confuse someone, not to test his/her knowledge (I suspect that this is an artificial interview questions, not a real one).
I'm new to ASP.NET and not the most experienced of programmers.
I have recently been introduced to ASP.NET-MVC 3 for an application I'd like to build.
I have the basic functionality down but am not to familiar with the login.
The built-in login works for what I want (just something simple), but I want to ensure that a login must be used before any of the actual functionality appears.
What would be the best way of doing this?
Any help would be greatly appreciated.
in your controller you should make use of the Authorize attribute, this forces the authorization before doing the decorated action.
For instance, in your home controller add the following [Authorize] as such
[Authorize]
public ViewResult Index()
{
return View();
}
Also you can decorate an entire controller which will force ALL methods to be authroized prior to being used, as such:
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
I have a view that can be accessed three ways:
1. Empty form - initial get from a menu
[HttpGet]
public ActionResult GetMe()
2. Populated form after a form submit (after a dropdown value is selected)
[HttpPost]
public ActionResult GetMe(int firstDropdownValue)
3. Populated after a request from another page via javascript. This is a GET.
[HttpGet]
public ActionResult GetMe(int secondDropdownValue, int thirdDropdownValue)
The problem is that #3 conflicts with #1 because two gets to the same ActionName are not allowed. I could give #3 a different action name, but I would really like both to display the same name in the URL.
I have looked at the [ActionName] attribute but I don't think it can help me. Is there a workaround?
#Andrew Barber is right. Forget about MVC for a second, you can't have two methods with the same signature. They can be combined to one.
[HttpGet]
public ActionResult GetMe()
[HttpPost]
[ActionName("GetMe")]
public ActionResult GetMeHttpPost(int dropDownValue)
This of course means you need to change your 3rd option (the JS) to do a POST instead of a GET. If that's a problem, you'll need a third action with a seperate URL.
Although i don't envision a scenario where JavaScript is required to "GET" an entire page - shouldn't it be a child action returning PartialViewResult?
After you fix the compile error, you can combine #1 and #3 with a nullable int.
[HttpGet]
public ActionResult GetMe(int? dropDownValue)
I have a problem with a sample routing with the preview 5 of asp.net mvc.
In the AccountController I have 2 actions:
public ActionResult Delete()
public ActionResult Delete(string username)
While trying to look for Account/Delete or Account/Delete?username=davide the ControllerActionInvoker throws a exception saying that Delete request is ambiguous between my tow actions methods.
The default route in the global.asax hasn't been changed.
Shouldn't the action invoker understand what's the method to call looking in the parameters list?
Using the preview 4 I hadn't these kind of problem performing the same operation.
Any idea?
Solution found!
With the introduction of the ActionNameAttribute, it's now necessary to filter manually which method to call depending on the request. This is done by the ActionSelectionAttribute.
Full explanation here: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx
I can't say for sure why this is happening. But you might want to consider only having the Delete(string username) action and removing the parameter-less overload.
Because string is nullable my understanding is that simply calling Account/Delete will invoke the action with a null username parameter which you can then test for at the beginning of the action method.
What I'd do is ditch the blank Delete(), and only use Delete(string username)
In your url routing you'd have something similar to "/{Controller}/{Action}/{username}/" ?
If you have "/{Controller}/{Action}/{Id}/" you'd be better off doing Delete(string id) and that way just using the url to handle this "/Account/Delete/davide/"
That said use your default route which should be something like the default Id is ""
Then in your Delete(string id) method have:
public ActionResult Delete(string id)
{
if(string.IsNullOrEmpty(id)) return EmptyID();
// Continue normal Delete method
}
public ActionResult EmptyID()
{
// The method you were going to have on a blank delete.
}
That or just wrap it up in the one method on an if {} else {}
Either way I'd just be going with the one method and doing a default on your username/id in your route of an empty string and handle it that way.
If you want to contact me on further follow up to what I mean, or whatever will help, ping me at andrew# my domain on my info page.
Edit: Ah pretty much what Berko said anyway, I'm not sure how Named Attributes would help - so please post a comment here detailing it for other guys who find the same issues! :)
Its ambiguous because the two controller action are the same post method..
You can only used that in form posting scenario for example you are submitting a form data that uses HTTP post..