I'm told that if I want to grab a claim within a controller I can do something like:
IClaimsIdentity u = (IClaimsIdentity) this.HttpContext.User.Identity;
var ni = u.Claims.First(x => x.ClaimType == ClaimTypes.NameIdentifier).Value;
however, this violates the separation between views and controllers. the controller may be called in a context where there is no HttpContext - so what is the proper way to do it?
TIA - ekkis
Just leave out the HttpContext and use the User property of the controller directly:
var u = (IClaimsIdentity)this.User.Identity;
Related
I have decided to use a common viewModel for a number of my screens. However to add to my flexibility I would like to at least know the name of the controller and action that called the viewModel / view.
Is it possible to get this information and how can I do it?
Thanks
var action = (string)ViewContext.Controller.ValueProvider.GetValue("action").RawValue;
var controller = (string)ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
I am wondering what is faster. Should i store information that i queried from DB into a ViewBag and then access and display it in a View, or should i make a query directly in View.
Controller is the middle layer in MVC so it would seem logical to me, that accessing DB in controller should be faster.
Example:
1) In a Controller i access the DB and put the information into a ViewBag so that i can display it in View.
controller:
EVENT e = db.EVENT.Find(id);
ViewBag.event = e;
if (e.poster_id != null)
{
poster = e.DATA.path;
}
ViewBag.dict_poster = poster;
view:
<p>POSTER: #ViewBag.dict_poster;</p>
2) In Controller i put only the general data from DB into ViewBag and that access it in View.
controller:
EVENT e = db.EVENT.Find(id);
ViewBag.event = e;
view:
<p>
#if(ViewBag.event.poster_id != null)
{
#ViewBag.event.DATA.path;
}
</p>
What is better and more appropriate to use? What is faster?
What if i have a very branched DB and i have to store everything into "ViewBag.e", is it better and faster to store individual values into ViewBags and than access them in Views or is direct access from a View faster?
What about Lists? (storing DB information into a list, putting it in a ViewBag and than accessing it in the View)
I am grateful for your answers and added explanation....
You shouldn't be accessing your data layer from the view directly, this should be done in the controller. Also, consider passing a strongly typed view model to your view, rather than using the ViewBag.
See http://theminimalistdeveloper.com/2010/08/21/why-when-and-how-to-use-typed-views-and-viewmodel-pattern-in-asp-net-mvc/ for reasons behind the use of view models in MVC.
When I try
user = System.Web.UI.Page.CurrentUser
or
user = System.Web.UI.Page.User.Identity
I get an error saying that the method is not defined for System.Web.UI.Page
I am trying to access it within a Controller, does that matter?
I checked that I do not have another class named Page, Why would it say the method is not defined?
There are many ways to do it (basically, they are all the same)
User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere
Page.User property works when there's a Page HTTP handler that's processing the current request. As in an MVC controller, the request has not been handed to a Page class, it won't work.
in the controller.
Edit: Looks like someone else beat me to it.
Found the answer here: ASP.NET Controller Base Class User.Identity.Name
HttpContext.User seemed to work alright... anyone see anything wrong with that?
CurrentUser doesn't seem to be a property on System.Web.UI.Page. (http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx)
Your controller should expose a property named User, does that work? For example:
var user = User;
I have a little question to community. Does ASP.NET MVC allow to execute an action of another controller without RedirectToAction() method and without creation of an instance of this controller?
This is impossible. For an instance method to execute there must be an instance to execute in. Action methods are simply methods like all other methods, so you always need an instance to call the method.
Do you mean you wish to have a View which returns nothing. No view. No nadda?
If so, u can return a EmptyResult ViewResult class...
HTH.
You can call it through the Reflection technic, But It's Not Recommended. Shortly because it's not in the request/response/controller context.
Personally, I prefer to have an internal static method(bla bla){...} and call it where ever i want.
But if you drop the
without creation of an instance of this controller
Clause, Then you can use this methodology. But as it explains, It's Not Recommended Too. the following is summarized:
var controller = new FooController();
controller.ControllerContext = new ControllerContext(this.ControllerContext.RequestContext, controller);
var jsonResult = controller.BlaMethod(someInputParams);
from my controller I want to assign a value to an application variable much like I would a session variable.
I assumed I could just use:
Application("MyValue") = "test"
HttpContext.Application["MyValue"] = test
If you are trying to use this to pass data from the controller to the view, use the ViewData.
ViewData("MyValue") = "test"
If not, please give more context as to what you are trying to accomplish.