accessing master page hyperlink from a different controller - asp.net-mvc-3

I am using ASP.NET 3.0 MVC with membership provider. I have to make modification to the default implementation we get with membership related code. I have to move LogOff Hyperlink in the master page. Initially this link is in-visible but once authentication is succesfful I need to make it visible. This action will happen from Controller for authentication and I am not sure how to access the hyperlink defined in a master page from a different controller. Any ideas how this could be achieved keeping the spirits of MVC design?

You could create another action in your AccountController to determine if the log off link needs to be displayed. This action has a partial for the html. Call this new action from the master page and check in that action if the log off link needs to be displayed.
#{ Html.RenderAction("displayLogOff", "Account"); }

I'm not sure why you need to make a modification, as this seems to be the default behaviour, but typically you would use a partial view to display the log off hyperlink. This partial view can run an action, and in this action you would send a model to the view which could include a flag for if the user is currently authenticated. Alternatively, you can just check Request.IsAuthenticated in the partial view directly.

I'm not sure I understand. The default implementation already does this. It works regardless of what controller you're using. The reason is that the master page uses a partial page that specifies the method and controller.

Related

MVC design issue - roles of each component

This is our first time using this pattern (we're not even sure that this pattern is suitable for this project), and me and my mates have a couple of questions.
We're designing a simple applications for displaying files with regards to permissions.
Suppose "Joe" is currently logged in to the system, where would be the correct place to save "Joe" as the active user?
What are the roles of the controller in this context? Suppose "Joe" wants to log in to the system. He enters his password and clicks log on, should the view validate the details directly with the Model? or should it ask the controller to do it for him?
Suppose that the login is successful, what should happen now? Should the View listen as an Observer to the model and wait for a response that the login is correct and then switch windows? or should the Controller have a method called boolean ValidateLogin() which the view calls and acts accordingly?
As you can see we're very confused, any help would be appreciated.
Session / Database / Configuration File (is this a single or multiple user system?)
In some web frameworks*: the view passes the login data to controller, passed to model which would validate, return the result to controller, and give the correct view as response. Surely it's also fine if the view wants to directly validate the data to the model, but IMO view-model communication should never have any logic involved, only data update notification.
I think it's partially answered in answer 2 above. The controller should be able to destroy/change the login view though (when the login successful, normally the view should change accordingly).
*This is because in web application, usually view cannot directly talk to model, as opposed in desktop application. This is still an MVC, only a variation called MVA.
For your requirements, I suggest you use this project MembershipStarterKit to start with.
It has samples that do basic authentifacation and role management.
It also use the .Net membership provider so that you don't re-invent the wheel.
Store it in session
Any client side validation can be done by the view directly with the model(Through DataAnnotations). And for database calls it goes through the contoller
On successful login, controller will render the corresponding view

ASP.NET MVC3: password protect a page

Hello in an MVC3 application,
I want to put a password on a page.
The page is accessible from an action link that calls an ActionResult in the controller. And returns the view with the model.
What is the best way or easiest way to add a password to the page.
Something like a popup that shows and you have to put a password and if you put the wrong password you don't see the page (the view is not loaded)
Thanks
You can put a filter on the page's get action.
look at the following link
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

Return View From Controller in Area Not Working

After reading several posts on this subject I have yet to find an answer to my problem. I have an MVC 3 application and have added an Area to it. Everything works great until I try an return a view from a controller within the Area.
I can successfully post to the controllers Save Method but upon simply returning the view (return View()) I get the following:
The view 'Save' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Test/Views/Default1/Save.aspx
~/Areas/Test/Views/Default1/Save.ascx
~/Areas/Test/Views/Shared/Save.aspx
~/Areas/Test/Views/Shared/Save.ascx
...
This seems so basic, not sure why I am running into so much trouble.
Ive used Phil Haack's RouteDebugger (http://nuget.org/packages/routedebugger) and all routes are working as setup...
MVC is expecting (by the convention) a view (with the same name as your action name in any one of the folders (by default, But you can override this). You should have the view in any of the folders. That is the MVC convention. So add your view to that folder. You can add it by right clicking the Return View() statmenet in your action method and selecting Add View option. It will automatically add one view.
Or you can right click on the Areas/Test/Views/Default1 folder and select Add View and save it with the same name as of your Action method. If you want to save it with a differnt name than the action name, you can use the View method like this
return View("MyOtherViewName");
Assuming that you added a MyOtherViewName.cshtml as your View in the Areas/Test/Views/Default1 folder

ASP.NET MVC3: Get method only accessible through other method

I'm using the standard forms authentication registration that comes with MVC 3.
I want to add a few checkboxes to that form that indicates what kind of user the registrator will be. Depending on the checkbox choice the user will be redirected to another get method where additional information can be added.
The problem is that I don't want these methods to be accessible in any other way (like typing the url for example). Only in this specific case these these methods can be accessed.
My first idea was to create a session in the post method of the first registration form and check in the get method if the session exists. But this seems like a less elegant way.
Does anyone has a safer solution for this problem?
You should be able to use the [ChildActionOnly] attribute: found here on MSDN.

MVC Deployment Problem - Site Loads but Links (Routes) do not

Having a problem deploying an MVC application.
Basically the site loads correctly, the home page appears. However anything which needs to access a controller action does not. So all the links just throw up 404 errors.
Does anyone have an Idea why the site loads but after that the controller actions appear not to?
Thanks
Are you running your app in IIS 6? If so you'll need to configure the .mvc extension or configure wildcard mapping. Steve Sanderson has a good post on it.
The fact that the home page appears indicates that you have at least one controller working properly. Namely, the HomeController.
You should check that you are following the default conventions (if you have it set up that way)
Controllers belong in the Controllers folder and follow the naming convention [Name]Controller.
Also, every action in the controller must be public and must return an ActionResult of some kind. Returning a View will cause a particular View to be rendered.
Also, Views follow the folder structure View/[ControllerName]/[Action].aspx
The fact that the first page loads means you probably have Home/Index set up properly for both your Controller and your View. You should take a look at those and see what the difference between that is and the other controllers/actions/views that you've set up.

Resources