This is my first attempt in creating an MVC program and my first month using ASP.NET.
So far I understand the basic from MVC framework. The software that I am trying to create is an online application, basically a survey in which the admin is able to modify the questions and members with a login are able to see the results from the survey.
I am currently stuck trying to figure out how my class diagram will look like.
So far the Model interacts with the database and contains the following classes
DAOSurvey (get and setter for the data)
SurveyModel (Execute all the queries)
SurveyQueries (Add and retrieve the results from the survey)
ISurvey (Interface to communicate with controller)
Now, I know that I need some view classes, it is correct to have a "Loginview" "Adminview" "Membersview" and "Surveyview" ? I should create a class for every asp page in my ASP.NET application?
Finally I need the controller classes, which I am guessing that Admin and Members should be.
I am quite sure about the model side of my class diagram should look like, however, I am lost about how view classes and controller classes should be. Any suggestions?.
Thanks in advance.
In ASP.NET MVC you will get features like Login and register automatically if you change the authentication to Individual user account while creating a new project or you can manually change it if you have already created the project.
Here you can create logins for different user roles and then use role based authorization for different controllers.
Related
Hi im a beginner in the use of asp.net framework and web development.
So im leaerning by starting a project.
Im using asp.net web api. Im having issues understanding how to use my
own entity framework codefirst database generated models, with the AppUserModel found in the
IdentityModel.cs.
The thing is that i have a class library where i defined a model
Staff. It contains properties like ID Username, password, Name,
and StaffCategory. For the staffCategory this is defined in another
file as an enum. Where the keys are Employee and Manager.
THis model is mapped to my existing database.
From the way i designed this model, i had intended to use it as my user account.
Also then in my code i intended to add roles EmployeeRole and ManagerRole corresponsng to StaffCategory.
But now on creating the asp.net web application i see that there is a user model already.
ApplicationUser. And is used for user account creation and authentication.
Im really confused now about how my model Staff fits in right now.
My questions are
1. Can i use my own model(Staff) for user account? If so is it best practice,
and how can i achieve this? And how do i mix roles with it?
2.If i cannot use my Staff model for user accounts. How do i use the ApplicationUser Model
together with my Staff model?
Sorry if my question is bulky. But i have pondered and researched for so long without staright answers to
these questions. So i just need to find out.
Ohh and example code for this would be really helpful for better understanding.
Thanks in advance.
I recently made my first ASP.NET website with MVC. I selected the option that pre-loads a basic project with login functionality and a few pages. I spent a couple hours learning how it all works and I'm pretty comfortable with most of the features. Now I want to add a class that interacts with the database and I've run into a bit of an issue.
When I search for a solution every response says to use DbContext. I don't think there's necessarily anything wrong with this, but when I search for DbContext and some other commands that show up frequently in these responses, there are no instances of them in the project. I would really like to use the same method of creating models that was done for the account classes, but when I look at the code I'm not entirely sure what I'm looking at as it links to a bunch of different files.
Can I get some tips on how to create classes the way that ASP.NET creates default account models?
For reference: I'm using Web Essentials, Productivity Power Tools and VS 2013.5
There's tons of tutorials online that cover ASP.NET MVC Code First Entity Framework. I start with something like this
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
And see where you get to. This is the basic approach:
Create the model classes
Create the Context
Enable Migrations (in case you need to change the model)
The SQL Expres database will be built automatically based on your EF classes.
Good luck. It's not a difficult as it first appears once you've done your first one.
Basically I'm working with an existing live MVC 3 application which has no way of tracking user logins/Activity.
It has been operating for some time now, so I cannot introduce Simplemembership and would be reluctant to start adding, out of the box solutions/libraries in case it has a knock-on effect elsewhere.
Is there any tutorials or examples out there that show adding a "History" table for example and how to populate it ?
Please note this is a asp.net MVC 3 applicatio, using Razor views and code first.
Cheers
Assuming you have a login facility already, you can simply extend your business logic to implement a new InsertHistory method and call this in the same controller function that handles the POST when logging in.
We're working on some kind of Cloud CMS using ASP.NET MVC technology, and have found some obstacles on the way. There is a number of parameters user could change thru the control panel that we need to end up in Views. For example, Facebook application id to initialize the Facebook JS API. Or additional text to be shown on the page. Or background picture. For now we're not using DI to transfer this parameters, instead we're adding them to the ViewModel, but this ruin the ASP.NET MVC way of working with models (e.g. form validation, bindings etc.)
It looks like that using DI to inject services for providing parameters, texts and pictures could make my views less dependent on controllers specific, and there is even some Microsoft technique to do it http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection#Exercise2. However, there are a lot of answers on forums against injecting services into Views using DI.
So the question: what is a right way to inject some services into Views? Or I shouldn't do it at all and something is wrong in the application design?
UPDATE: some real code examples (now we're using Model to inject the services)
Injecting texts from database (they have to be user-editable, as it is CMS):
<div class="steps">#Html.Raw(Model.Texts["Main", "Step2"]</div>
Injecting translations from database (actually, it is localization):
<div class="gonfalon">#Model.Translations["Title_Winners"]</div>
Injecting parameters (from database, could be request-specific; for example, if the site has different domains, facebook application should be per-domain):
Facebook.Initialize(Model.Parameters["FbApplicationId"], Model.Parameters["FbApplicationSecret"]);
The problem of current approach is that this code has taken from contest mechanic. It is definitely out of contest business scope to deal with custom texts, translations or facebook application Id. Also it ruins the Model as model models not actual business domain but deals with a lot of things actually belongs to View (like translations and custom texts)
UPDATE 2: Have modified the snippet from the answer below to be a bit more generic:
public static class WebViewPageExtensions
{
public static I ResolveService<I>(this WebViewPage page)
{
return DependencyResolver.Current.GetService<I>();
}
}
No, you shouldn't inject services into Views, but...
For scenarios such as theming where you want to give the theme developer more power, just one model isn't enough. If your model contains the current post for example, how can a theme designer asks for a list of categories for the sidebar? Or for a widget?
In asp.net mvc you can use extension methods to offer that functionality.THe extension method will use the dependency resolver to get the service. This way, you can have the needed functionality in the view without actually injecting a service.
Note that calling the business layer to update the model is still a violation of Separation of Concerns. THe services made available to the view should contain only read model or general utility functionality.
An example
public static IMyViewServices MyServices(this WebViewPage view)
{
return DependencyResolver.Current.GetService<IMyViewServices>();
}
IMyViewServices lifetime configured in the DI Container should be per http (scope) request
No, end of story. Why? Here is why:
Your view only needs to know what the view model it's going to be working with to present that model. There are couple of reasons for this but the biggest one is the separation of concerns. Keep your view as stupid as possible. You will see that this seperation will give you a clean application structure throughout the way.
There is a number of parameters user could change through the control panel that we need to end up in Views.
I'm not sure what you exactly mean here but this is why there are view models. Your business layer will shape models up, your controller will simply map them to your view models and pass them into the view (presentation layer).
This really depends on how much or little you want your controllers to do and to what degree of separation you want to achieve.
In my world, the "controller" in an MVC app does as little as possible because I have a service layer handling all of the business logic and a data layer above that handling all of the database interaction.
On a GET, the controller will simply call a service method that will build the view model and hands it back to the controller and the controller passes it on to the view. On POST, the view posts data to the controller which sends it off to the service layer for validation, saving to DB, etc. The service is injected into the controller's constructor.
I'd be more than happy to post code examples if you'd like to see them.
I'm learning ASP.NET MVC 3 to build a web application. I've followed a couple tutorials teaching the Entity framework, Razor, MVC, etc. One thing I can't find much about though is how to do authentication and authorization. In the tutorials I can find, it basically goes
Create an "Internet Application" project. The authorization system is already there so you don't need any code. Now use an ASP.NET settings panel to set up user roles and slap [Authorize] everywhere.
I understand the system it starts you off with is called ASP.NET Membership. Is it common to just stick with that for your authorization?
I've been using Code First to do models and databases -- is there a way I can create my own user models, have ASP.NET create the database, and use that for membership? How tough is it to customize user models (for example, if I want some user roles to have some information stored with their membership, but not others)?
Are there any tutorials that go through ASP.NET Membership in detail rather than just starting with whatever the project template spits out? For example, implementing it in an empty project?
It depends on the needs of your application. You will find the provided model has some limitations, but if you only need basic user/role management then it might suffice. Personally, I've always found it a bit too inflexible for my liking, but if it does what you need for the application your building, don't waste your time reinventing it.
Hope this helps.
You can extend the default membership behavior by adding a role provider which allows you to group users into one or more roles.
Furthermore you can extend the user's profile information to store additional information (although not specific to a role, so all profiles will have a slot for role-specific information). See this article by ScottGu for how to extend a profile: http://weblogs.asp.net/scottgu/archive/2005/10/18/427754.aspx.