Skip to content

Commit

Permalink
cleaned up the Startup class and updated README to reflect changes to…
Browse files Browse the repository at this point in the history
… Core
  • Loading branch information
markentingh committed May 5, 2018
1 parent 9dbaa96 commit ad2ccc6
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 330 deletions.
4 changes: 2 additions & 2 deletions Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Page(HttpContext context) : base(context){}
public virtual string Render(string[] path, string body = "", object metadata = null)
{
//renders HTML layout
var scaffold = new Scaffold("/layout.html", Server.Scaffold);
var scaffold = new Scaffold("/Views/Shared/layout.html", Server.Scaffold);
scaffold.Data["title"] = title;
scaffold.Data["description"] = description;
scaffold.Data["head-css"] = headCss.ToString();
Expand All @@ -39,7 +39,7 @@ protected virtual string AccessDenied(bool htmlOutput = true, Page login = null)
{
return login.Render(new string[] { });
}
var scaffold = new Scaffold("/access-denied.html", Server.Scaffold);
var scaffold = new Scaffold("/Views/access-denied.html", Server.Scaffold);
return scaffold.Render();
}
return "Access Denied";
Expand Down
77 changes: 31 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Datasilk Core
#### An MVC Framework for ASP.NET Core
A ultra-fast, light-weight alternative to ASP.NET Core MVC 5 that supports HTML scaffolding and simple web services.
Datasilk Core is an ultra-fast, light-weight alternative to ASP.NET Core MVC 5 that supports HTML scaffolding and simple web services.

Instead of managing a complex ASP.NET Core web application and all of its configuration, simply include this framework within your own ASP.NET Core Web Application project, follow the installation instructions below, and start building your website!

Expand All @@ -17,55 +17,45 @@ Instead of managing a complex ASP.NET Core web application and all of its config
* update the `namespace` value to reflect your web application's namespace. This will allow Datasilk Core to access code from your project correctly
* update the `data/SqlServerTrusted` value to connect to your SQL Server database.

3. copy `/Core/layout.html` into the root of your ASP.NET Core project. You can make edits to this file if you need to add custom HTML within the `<head>` tag or the foot of your website layout.
3. copy `/Core/layout.html` to `/Views/Shared/layout.html`. You can make edits to this file if you need to add custom HTML within the `<head>` tag or the foot of your website layout.

4. copy `/Core/access-denied.html` into the root of your ASP.NET Core project.
4. copy `/Core/access-denied.html` to `/Views/access-denied.html`.

5. Open your `/Startup.cs` class file and replace everything with:
```
public class Startup: Datasilk.Startup{ }
```
```
public class Startup: Datasilk.Startup{ }
```

6. Create a new class `/Routes.cs` in the root of your ASP.NET Core project and replace everything with:
```
using Microsoft.AspNetCore.Http;
using Datasilk;
6. Create a new class `/Routes.cs` and replace everything with:
```
using Microsoft.AspNetCore.Http;
using Datasilk;

public class Routes : Datasilk.Routes
{
public Routes(HttpContext context) : base(context) { }
}
```
public class Routes : Datasilk.Routes {}
```
> NOTE: You can set up your routes once you start creating Page controllers

7. Open your *Project Properties*, select the *Application* tab and change *startup object* to use `Datasilk.Program`

8. In Visual Studio, change the properties for all `.html` files to copy to Output Folder (if newer). This will ensure that these files are copied when *publishing* your web application to IIS

That's it! Next, learn how to use the Datasilk Core MVC framework to build web pages & web services.

## Page Requests

All page request URLs are mapped to classes that inherit the `Datasilk.Page` class located in the `Pages` namespace for your project (e.g. `MyProject.Pages`). For example, the URL `http://localhost:7770/products` maps to the class `MyProject.Pages.Products`.
All page request URLs are mapped to controllers that inherit the `Datasilk.Page` class located in the `Pages` namespace for your project (e.g. `MyProject.Pages`). For example, the URL `http://localhost:7770/products` maps to the class `MyProject.Pages.Products`.

> NOTE: Replace "MyProject" with the name of your project in the examples above & below
### Example

**/Views/Home/home.html**
```
<html>
<head>
<title>{{title}}</title>
<description>{{description}}</description>
</head>
<body>
{{content}}
</body>
</html>
<div class="hero">
<h1>{{title}}</h1>
<h3>{{description}}</h3>
</div>
```
**home.html**

#

**/Controllers/Home.cs**
```
using Microsoft.AspNetCore.Http;
Expand All @@ -78,42 +68,39 @@ namespace MyProject.Pages
public override string Render(string[] path, string body = "", object metadata = null)
{
//render page
var scaffold = new Scaffold("/Pages/Home/home.html");
var scaffold = new Scaffold("/Pages/Home/home.html", Server.Scaffold);
scaffold.Data["title"] = "Welcome";
scaffold.Data["description"] = "I like to write software";
scaffold.Data["content"] = "<h1>Hello World!</h1>";
AddScript("/js/views/home/home.js");
return base.Render(path, scaffold.Render(), metadata);
}
}
}
```
**/Pages/Home/Home.cs**

#

In the example above, a user tries to access the URL `http://localhost:7770/`, which (by default) will render the contents of the `MyProject.Pages.Home` class. This class loads `/Pages/Home/home.html` into a `Scaffold` object and replaces the `{{title}}` variable located within the `home.html` file with the text "Welcome!". Then, the page returns `base.Render`, which will render HTML from `/layout.html` along with the contents of `scaffold.Render()`, injected into the `<body>` tag of `/layout.html`.
In the example above, a user tries to access the URL `http://localhost:7770/`, which (by default) will render the contents of the `MyProject.Pages.Home` class. This class loads `/Views/Home/home.html` into a `Scaffold` object and replaces the `{{title}}` variable located within the `home.html` file with the text "Welcome!". Then, the page returns `base.Render`, which will render HTML from `Views/Shared/layout.html` along with the contents of `scaffold.Render()`, injected into the `<body>` tag of `Views/Shared/layout.html`.

> NOTE: `MyProject.Pages.Home` is the default class that is instantiated if the URL contains a domain name with no folder structure.
> NOTE: `MyProject.Pages.Home` is the default class that is instantiated if the URL contains a domain name with no path structure.
### Page Hierarchy
To render web pages based on complex URL paths, the Datasilk Core framework relies heavily on the first part of the request path to determine which class to instantiate. For example, if the user accesses the URL `http://localhost:7770/blog/2018/01/21/Progress-Report`, Datasilk Core initializes the `MyProject.Pages.Blog` class.

The request path is split up into an array and passed into the overridable `Render` function located in your `Datasilk.Page` class. The `paths` array is used to determine what type of content to load for the user. If we're loading a blog post like the above example, we can check the path to find year, month, and day, followed by the title of the blog post.
The request path is split up into an array and passed into the overridable `Render` function located in your `Datasilk.Page` class. The `paths` array is used to determine what type of content to load for the user. If we're loading a blog post like the above example, we can check the `paths` array to find year, month, and day, followed by the title of the blog post, and determine which blog post to load.

### Datasilk.Page
Inherited in classes that are used to render page requests.

### Layout.html
`/layout.html` contains the `<html>`, `<head>` & `<body>` tags for the page, along with `<meta>` tags, `<link/>` tags for CSS, and `<script>` tags or Javascript files.
`Views/Shared/layout.html` contains the `<html>`, `<head>` & `<body>` tags for the page, along with `<meta>` tags, `<link/>` tags for CSS, and `<script>` tags or Javascript files.

### Access Denied
If your web page is secure and must display an `Access Denied` page, you can use:

```return AccessDenied(true, Login(S))```

from within your `Datasilk.Page` class `Render` method, which will return the contents of the file `/access-denied.html`. If a `Datasilk.Page` class is supplied (e.g. `Login(S)`), instead of loading `/access-denied.html`, it will render an instance of your `Datasilk.Page` class.
from within your `Datasilk.Page` class `Render` method, which will return the contents of the file `Views/access-denied.html`. If a `Datasilk.Page` class is supplied (e.g. `Login(S)`), instead of loading `Views/access-denied.html`, it will render an instance of your `Datasilk.Page` class.

> NOTE: You can find more functionality for the `Page` class inside `/Core/Request/Page.cs`.
> NOTE: You can find more functionality for the `Page` class inside `/Core/Page.cs`.
## Web Services
The Datasilk Core framework comes with the ability to call `RESTful` web APIs. All web API calls are executed from `Service` classes located in the `Services` namespace within your project (e.g. `MyProject.Services`) and will inherit the `Datasilk.Service` class.
Expand Down Expand Up @@ -163,9 +150,7 @@ using Datasilk;
public class Routes : Datasilk.Routes
{
public Routes(HttpContext context) : base(context) { }
public override Page FromPageRoutes(string name)
public override Page FromPageRoutes(HttpContext context, string name)
{
switch (name)
{
Expand All @@ -176,7 +161,7 @@ public class Routes : Datasilk.Routes
return null;
}
public override Service FromServiceRoutes(string name)
public override Service FromServiceRoutes(HttpContext context, string name)
{
return null;
}
Expand Down
7 changes: 2 additions & 5 deletions Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ namespace Datasilk
{
public class Routes
{
protected HttpContext context;
public Routes(HttpContext context) { this.context = context; }

public virtual Page FromPageRoutes(string name)
public virtual Page FromPageRoutes(HttpContext context, string name)
{
return null;
}

public virtual Service FromServiceRoutes(string name)
public virtual Service FromServiceRoutes(HttpContext context, string name)
{
return null;
}
Expand Down
15 changes: 13 additions & 2 deletions Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum Environment
//server properties
public DateTime serverStart = DateTime.Now;
public double requestCount = 0;
public double pageRequestCount = 0;
public double apiRequestCount = 0;
public float requestTime = 0;

//config properties
Expand All @@ -35,7 +37,7 @@ public enum Environment
public bool resetPass = false; //force admin to reset password
public Dictionary<string, string> languages;

private static string _path = "";
public static string _path = "";

//Dictionary used for caching non-serialized objects, files from disk, or raw text
//be careful not to leak memory into the cache while causing an implosion!
Expand All @@ -47,8 +49,17 @@ public enum Environment
// where data is injected in between each array item.
public Dictionary<string, SerializedScaffold> Scaffold = new Dictionary<string, SerializedScaffold>();

public string RootPath
{
set
{
//set the root path of the server
_path = value;
}
}

public static string MapPath(string strPath = "") {
if (_path == "") { _path = Path.GetFullPath(".") + "\\"; }
//if (_path == "") { _path = Path.GetFullPath(".") + "\\"; }
var str = strPath.Replace("/", "\\");
if (str.Substring(0, 1) == "\\") { str = str.Substring(1); }
return _path + str;
Expand Down
4 changes: 0 additions & 4 deletions Service.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Utility.Serialization;

namespace Datasilk
{
public class Service : Request
{
public Dictionary<string, string> Form = new Dictionary<string, string>();
public IFormFileCollection Files;

public enum injectType
{
replace = 0,
Expand Down
Loading

0 comments on commit ad2ccc6

Please sign in to comment.