125 lines
3.5 KiB
Plaintext
125 lines
3.5 KiB
Plaintext
== ASP.NET ==
|
|
|
|
ASP.NET is an open source cross platform web framework for C#
|
|
|
|
== [[MVC]] ==
|
|
|
|
See [[MVC]] as a concept.
|
|
|
|
The MVC model as it pertains to ASP.NET is very straightforward. There are
|
|
Models, which consist of objects, gernerally in the APPNAME.Models namespace.
|
|
Next there are Controllers, which are classes with methods for each operations
|
|
on the site (IE view/create), etc.
|
|
|
|
|
|
== Site structure ==
|
|
|
|
For some controller, XYZController, the base, or Index() of the controller, can
|
|
be accessed via mysite.com/XYZ. For other methods on the site, such as create, one must call
|
|
mysite.com/XYZ/Create. This applies for all methods in the Controller class
|
|
following signature `public IActionResult MyCustomMethod()`. These usually
|
|
end with a `return View()`. These methods are called Actions or Action Methods.
|
|
|
|
== Models ==
|
|
|
|
Models are objects in asp.net that represent entries in database tables, or
|
|
data to be processed by the backend of the application.
|
|
|
|
=== Model validation ===
|
|
|
|
Member values in a model can be validated via validation attributes. These
|
|
attributes are
|
|
|
|
* Required
|
|
* StringLength(x)
|
|
* DataType(type)
|
|
* Display(Name = "Some display name")
|
|
* Range(0,100)
|
|
* ValidateNever
|
|
* CreditCard
|
|
* EmailAddress
|
|
* Phone
|
|
* Url
|
|
* etc...
|
|
|
|
All of the validation attributes are placed in square brackets one lines above
|
|
the member values of the model. For example,
|
|
|
|
{{{
|
|
[Phone]
|
|
[Required]
|
|
public string phoneNumber { get; set; }
|
|
}}}
|
|
|
|
All validation attributes can also be passed an argument of name `ErrorMessage`
|
|
of type string, and will be displayed on the webpage when the value is not
|
|
given.
|
|
|
|
== Controllers ==
|
|
|
|
=== Return types ===
|
|
|
|
* `View()`
|
|
* Returns the associated view
|
|
* Can be passed a model for data to be parsed by the view
|
|
* `RedirectToAction("Index")`
|
|
* Redirects user to some page
|
|
* Page is specified via the name of the Function as the Paramater
|
|
|
|
=== Action paramaters ===
|
|
|
|
Each action can take paramaters. These paramaters are URL paramaters and
|
|
their names match those sent in the URL. For example,
|
|
|
|
`public IActionResult MyAction(string name)` would take the paramater
|
|
`somesite.xyz/SomeController/MyAction?name=some_name_string`. In the controller,
|
|
the passed in arguments `name` would hold value `some_name_string` in this
|
|
instance.
|
|
|
|
== Views ==
|
|
|
|
A cshtml block may be created using a `@{}` block. Here C# can be placed,
|
|
however it is always ran by an interpretter at runtime, and is therefore not
|
|
recomended often.
|
|
|
|
=== ActionLink ===
|
|
|
|
The `@Html.ActionLink()` function can be placed inside of a `<form>` block in a
|
|
cshtml page. The arguments are as follows
|
|
|
|
{{{
|
|
@Html.ActionLink("Text Seen on page",
|
|
"Controller",
|
|
"Action Method",
|
|
new { URLPARAMNAME = 1 }, null)
|
|
}}}
|
|
|
|
Action Links are prefered, as then links do not need to be hardcoded into the
|
|
application.
|
|
|
|
=== Passing data to views ===
|
|
|
|
A view can take paramaters, primarily a single object, via passing it to the
|
|
`View()` constructor. In the .cshtml file (the view), one must then add the
|
|
line `@model APPNAME.Models.SomeModel` to specify the exact model type being
|
|
passed in.
|
|
|
|
NOTE: `@using APPNAME.Models` is required at the top of the view
|
|
|
|
=== Showing Controller data in views ===
|
|
|
|
A view can display data from a passed model via the `@Model.MEMBER_VALUE`
|
|
syntax anywhere in the HTML.
|
|
|
|
=== ViewBag ===
|
|
|
|
The ViewBag is an object that can be accessed in the Controller, and any sub
|
|
type can be apecified. IE `ViewBag.Title = "Some title"` is valid, without
|
|
declaring Title as a string or even existing first.
|
|
|
|
== Common Errors ==
|
|
|
|
=== AspNetCore.App version 6.0 not found ===
|
|
|
|
Install the runtime, `aspnet-runtime`
|