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
}
Related
In my MVC 4 site, authenticaiton is configured for intranet.
Now, i want to get logged in UserID,
How to fetch the user ID at some common place when accessing any first action (on particular user session) ?
Also, how to fetch User ID existed in context ?
Currently, i wrote session set logic User.Identity.Name on each controller index action.
Is this possible to move that logic for session set into some common place ?
Sorry for confusion.
Got the solutio. Created Action Filter as below and applied at each controller as attribute so, that code execute each time and i added relatd stuff at this place:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ControllerLogAndAccessFilter : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
}
I am using Identity in my MVC project. Nowadays, i am trying to develop my authentication system and implement group-based identity.
I wonder what the way is, are you following to manage roles. IE. i have an action in one of my controller and named, CreateNewStudent. I am creating new role names, CanCreateStudent and write the related action filter, top of my action.
Is that the only way ? Should i need to put action filters one by one ?
Regards.
You could also have those attributes on controllers. In the code below for example, if the controller inherited from the base, the actions inside of that will require authentication. You can add roles in there.
[Authorize]
public class BaseController : Controller
{
//do some common operations
}
public class HomeController : BaseController
{
public virtual ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
}
I am new to MVC.I am having some doubts so please clarify me.
How to store large data in MVC4 and how to pass that data across the pages.
How to maintain user details across the pages. In webforms we are having sessions but in mvc4 how we will do.
if we are having two actionresult of same name one will be fired on POSt action. How the CLR identifies which Action method to be called..means how it will identify that POSt method is called.
Define "large data" - remember that the web is stateless, persistence is done using a database or a server-side cache. I need more information about what you want to accomplish here.
ASP.NET MVC still supports Sessions. You can access the Session collection from any Controller action.
The Post action method must have a different method signature. The usual approach is to specify the view's model as a parameter, or a FormValueCollection, for example:
-
// GET
public ActionResult Foo() {
}
// POST
[HttpPost]
public ActionResult Foo(FooModel model) {
}
OR:
// POST
[HttpPost]
public ActionResult Foo(FormValueCollection postValues) {
}
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?
Given an ASP.NET MVC Controller class declaration:
public class ItemController : Controller
{
public ActionResult Index()
{
// ...
}
public ActionResult Details()
{
// ...
}
[Authorize(Roles="Admin, Editor")]
public ActionResult Edit()
{
// ...
}
[Authorize(Roles="Admin")]
public ActionResult Delete()
{
// ..
}
}
I need to reflect a list of methods in this class which may be invoked with the current user's permissions.
Please share some ideas of what could be done in this case.
Well for the new question think something along the lines of:
new ReflectedControllerDescriptor(typeof(ItemController)).GetCanonicalActions()
could be used to return the list of all available actions. I don't have ASP.NET MVC available to me at work, so I can't really check to see if the ActionDescriptor's returned by that will contain some parameter which says which members are allowed to execute them.
http://msdn.microsoft.com/en-us/library/system.web.mvc.actiondescriptor_members%28v=VS.90%29.aspx
That is the members of the ActionDescriptor, you might be able to find something in there. I'll see tonight if I can figure it out, this has gotten me kind of intrigued.
There's no universal user login/authentication system for all applications, thus this really isn't possible to create a 'universal solution'. You could create your own user login and authorization classes which you then add your own annotations to methods to do, but its going to have the same restrictions that the asp.net mvc system has, its only for your login/authorization system (or whoever extends that system).