|
Dynamic Data UI
|
First, you would need to install the NuGet package Microsoft ASP.NET DynamicData EFProvider. The package will add some templates for generating the views based on your data context. But we will need more than that to make it work.
|
The templates
|
|
DynamicData EFProvider package
|
Having the DynamicData folder from the package, now we need to create a new ASP.NET Dynamic Data Entities Web Application to get some files from it.
After creating the project, let's copy these files and folders to the DynamicData folder in our website:
Default.aspx, Default.aspx.cs, Site.master, Site.master.cs, Site.css, and Web.config; Content and Filters folders.
- In Site.master
- Change "~/Site.css" to "Site.css" (because the files are now in the same folder).
- The href for "Back to home page" is for the homepage of our administration UI, let's change it to "~/dbadmin/home" (I use dbadmin/{pagename} for routeUrl, because using dbadmin only will mess up with the default route).
- Add ~/ before DynamicData/Content/Images/back.gif.
- Change "~/Site.master" to "Site.master" for Default.aspx.
- Change "~/Site.master" to "..\Site.master" for all the views in
PageTemplates
. - Because ASP.NET Dynamic Data is WebForms, we need to install the package AspNet.ScriptManager.jQuery in order for the
asp:ScriptManager
controls to work.
Create a new class called Registration
in your DynamicData
folder. Change ApplicationDbContext
to the data context you want to use for generating tables and fields for data management.
using System.Web.DynamicData;
using System.Web.Routing;
using DynamicDataSample.Models;
using Microsoft.AspNet.DynamicData.ModelProviders;
namespace DynamicDataSample.DynamicData
{
public class Registration
{
public static readonly MetaModel DefaultModel = new MetaModel();
public static void Register(RouteCollection routes)
{
DefaultModel.RegisterContext(
new EFDataModelProvider(() => new ApplicationDbContext()),
new ContextConfiguration { ScaffoldAllTables = true });
routes.Insert(
0,
new DynamicDataRoute("dbadmin/{table}/{action}")
{
Constraints = new RouteValueDictionary
(new { action = "List|Details|Edit|Insert" }),
Model = DefaultModel
});
routes.MapPageRoute(
"dd_default",
"dbadmin/{pagename}",
"~/DynamicData/Default.aspx");
}
}
}
Here is how your DynamicData folder should be at this point:
Build the project, you will notice there's an error in Default.aspx.cs, we need to change the Global class to our new Registration class to fix:
One more step: call the Register
method for Dynamic Data in your Global.asax.cs, notice that it must be before the RegisterRoutes
:
If everything is correct, you will see the Dynamic Data interface when you run the website and go to /dbadmin/home:
For authorization, add something like this to the Site.master.cs:
protected override void OnInit(EventArgs e)
{
if (!Page.User.Identity.IsAuthenticated)
{
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
Response.End();
}
if (!Page.User.IsInRole("Admin"))
{
Response.StatusCode = (int)HttpStatusCode.Forbidden;
Response.End();
}
base.OnInit(e);
}
Reference
Professional ASP.NET MVC 4 is a great book, but it's not so great on Dynamic Data. I needed quite a few modifications, Googling around, and head-scratching... Hopefully, they will have some complete NuGet package for this in the future.