diff --git a/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs b/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs new file mode 100644 index 0000000000..e4cd7d39fd --- /dev/null +++ b/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web.Filters +{ + public class AgeEnhancerAttribute : ActionFilterAttribute + { + public async override Task Invoke(ActionFilterContext context, Func next) + { + object age = null; + + if (context.ActionParameters.TryGetValue("age", out age)) + { + if (age is int) + { + var intAge = (int) age; + + if (intAge < 21) + { + intAge += 5; + } + else if (intAge > 30) + { + intAge = 29; + } + + context.ActionParameters["age"] = intAge; + } + } + + await next(); + } + } +} diff --git a/samples/MvcSample.Web/Filters/InspectResultPageAttribute.cs b/samples/MvcSample.Web/Filters/InspectResultPageAttribute.cs new file mode 100644 index 0000000000..8ca5a20c6c --- /dev/null +++ b/samples/MvcSample.Web/Filters/InspectResultPageAttribute.cs @@ -0,0 +1,27 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; +using MvcSample.Web.Models; + +namespace MvcSample.Web.Filters +{ + public class InspectResultPageAttribute : ActionResultFilterAttribute + { + public async override Task Invoke(ActionResultFilterContext context, Func next) + { + ViewResult viewResult = context.Result as ViewResult; + + if (viewResult != null) + { + User user = viewResult.ViewData.Model as User; + + if (user != null) + { + user.Name += "**" + user.Name + "**"; + } + } + + await next(); + } + } +} diff --git a/samples/MvcSample.Web/Filters/PassThroughAttribute.cs b/samples/MvcSample.Web/Filters/PassThroughAttribute.cs new file mode 100644 index 0000000000..f13024ee3a --- /dev/null +++ b/samples/MvcSample.Web/Filters/PassThroughAttribute.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web +{ + public class PassThroughAttribute : AuthorizationFilterAttribute + { + public async override Task Invoke(AuthorizationFilterContext context, Func next) + { + await next(); + } + } +} diff --git a/samples/MvcSample.Web/Filters/UserNameProvider.cs b/samples/MvcSample.Web/Filters/UserNameProvider.cs new file mode 100644 index 0000000000..32c18b2cee --- /dev/null +++ b/samples/MvcSample.Web/Filters/UserNameProvider.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web.Filters +{ + public class UserNameProvider : ActionFilterAttribute + { + private static readonly string[] _userNames = new[] { "Jon", "David", "Goliath" }; + private static int _index; + + public override async Task Invoke(ActionFilterContext context, Func next) + { + object originalUserName = null; + + context.ActionParameters.TryGetValue("userName", out originalUserName); + + var userName = originalUserName as string; + + if (string.IsNullOrWhiteSpace(userName)) + { + context.ActionParameters["userName"] = _userNames[(_index++)%3]; + } + + await next(); + } + } +} diff --git a/samples/MvcSample.Web/FiltersController.cs b/samples/MvcSample.Web/FiltersController.cs new file mode 100644 index 0000000000..afaf336f95 --- /dev/null +++ b/samples/MvcSample.Web/FiltersController.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNet.Mvc; +using MvcSample.Web.Filters; +using MvcSample.Web.Models; + +namespace MvcSample.Web +{ + [ServiceFilter(typeof(PassThroughAttribute), Order = 1)] + [ServiceFilter(typeof(PassThroughAttribute))] + [PassThrough(Order = 0)] + [PassThrough(Order = 2)] + [InspectResultPage] + [UserNameProvider(Order = -1)] + public class FiltersController : Controller + { + private readonly User _user = new User() { Name = "User Name", Address = "Home Address" }; + + // TODO: Add a real filter here + [ServiceFilter(typeof(PassThroughAttribute))] + [AgeEnhancer] + public IActionResult Index(int age, string userName) + { + if (!string.IsNullOrEmpty(userName)) + { + _user.Name = userName; + } + + _user.Age = age; + + return View("MyView", _user); + } + } +} \ No newline at end of file diff --git a/samples/MvcSample.Web/Home2Controller.cs b/samples/MvcSample.Web/Home2Controller.cs new file mode 100644 index 0000000000..048d49ca34 --- /dev/null +++ b/samples/MvcSample.Web/Home2Controller.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.Mvc; +using MvcSample.Web.Models; + +namespace MvcSample.Web.RandomNameSpace +{ + public class Home2Controller + { + private User _user = new User() { Name = "User Name", Address = "Home Address" }; + + public Home2Controller(IActionResultHelper actionResultHelper) + { + Result = actionResultHelper; + } + + public IActionResultHelper Result { get; private set; } + + public HttpContext Context { get; set; } + + public string Index() + { + return "Hello World: my namespace is " + this.GetType().Namespace; + } + + public IActionResult Something() + { + return new ContentResult + { + Content = "Hello World From Content" + }; + } + + public IActionResult Hello() + { + return Result.Content("Hello World"); + } + + public void Raw() + { + Context.Response.WriteAsync("Hello World raw"); + } + + public IActionResult UserJson() + { + var jsonResult = Result.Json(_user); + jsonResult.Indent = false; + + return jsonResult; + } + + public User User() + { + return _user; + } + } +} \ No newline at end of file diff --git a/samples/MvcSample.Web/HomeController.cs b/samples/MvcSample.Web/HomeController.cs index 42854ec242..a0e88e211e 100644 --- a/samples/MvcSample.Web/HomeController.cs +++ b/samples/MvcSample.Web/HomeController.cs @@ -1,17 +1,63 @@ using Microsoft.AspNet.Mvc; +using MvcSample.Web.Models; namespace MvcSample.Web { public class HomeController : Controller { - public string Index() + public IActionResult Index() { - return "Hello from the new MVC"; + return View("MyView", User()); } - public IActionResult Test() + /// + /// Action that exercises query\form based model binding. + /// + public IActionResult SaveUser(User user) { - return View(); + return View("MyView", user); + } + + /// + /// Action that exercises input formatter + /// + public IActionResult Post([FromBody]User user) + { + return View("MyView", user); + } + + public IActionResult Something() + { + return new ContentResult + { + Content = "Hello World From Content" + }; + } + + public IActionResult Hello() + { + return Result.Content("Hello World"); + } + + public void Raw() + { + Context.Response.WriteAsync("Hello World raw"); + } + + public User User() + { + User user = new User() + { + Name = "My name", + Address = "My address" + }; + + return user; + } + + public IActionResult MyView() + { + return View(User()); } } } \ No newline at end of file diff --git a/samples/MvcSample.Web/LinkController.cs b/samples/MvcSample.Web/LinkController.cs new file mode 100644 index 0000000000..c68ec8c5dd --- /dev/null +++ b/samples/MvcSample.Web/LinkController.cs @@ -0,0 +1,33 @@ + +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web +{ + public class LinkController : Controller + { + public IActionResult Details() + { + return View(); + } + + public string About() + { + return Url.Action(null); + } + + public string Get() + { + return Url.Route(new { controller = "Home", action = "Details" }); + } + + public string Link1() + { + return Url.Action("Index", "Home"); + } + + public string Link2() + { + return Url.Action("Link2"); + } + } +} diff --git a/samples/MvcSample.Web/Models/User.cs b/samples/MvcSample.Web/Models/User.cs new file mode 100644 index 0000000000..9869e92d13 --- /dev/null +++ b/samples/MvcSample.Web/Models/User.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace MvcSample.Web.Models +{ + public class User + { + [Required] + [MinLength(4)] + public string Name { get; set; } + public string Address { get; set; } + public int Age { get; set; } + } +} \ No newline at end of file diff --git a/samples/MvcSample.Web/OverloadController.cs b/samples/MvcSample.Web/OverloadController.cs new file mode 100644 index 0000000000..97838a7e87 --- /dev/null +++ b/samples/MvcSample.Web/OverloadController.cs @@ -0,0 +1,30 @@ + +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web +{ + public class OverloadController + { + private readonly IActionResultHelper _result; + + public OverloadController(IActionResultHelper result) + { + _result = result; + } + + public IActionResult Get() + { + return _result.Content("Get()"); + } + + public IActionResult Get(int id) + { + return _result.Content("Get(id)"); + } + + public IActionResult Get(int id, string name) + { + return _result.Content("Get(id, name)"); + } + } +} diff --git a/samples/MvcSample.Web/SimplePocoController.cs b/samples/MvcSample.Web/SimplePocoController.cs new file mode 100644 index 0000000000..ae15d8bb22 --- /dev/null +++ b/samples/MvcSample.Web/SimplePocoController.cs @@ -0,0 +1,10 @@ +namespace MvcSample.Web +{ + public class SimplePocoController + { + public string Index() + { + return "Hello world"; + } + } +} diff --git a/samples/MvcSample.Web/SimpleRest.cs b/samples/MvcSample.Web/SimpleRest.cs new file mode 100644 index 0000000000..b2d9407adb --- /dev/null +++ b/samples/MvcSample.Web/SimpleRest.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web +{ + public class SimpleRest : Controller + { + public string Get() + { + return "Get method"; + } + } +} diff --git a/samples/MvcSample.Web/Startup.cs b/samples/MvcSample.Web/Startup.cs index 7005ce2aa7..e885d7a632 100644 --- a/samples/MvcSample.Web/Startup.cs +++ b/samples/MvcSample.Web/Startup.cs @@ -1,21 +1,24 @@ - +#if NET45 +using System; +using Autofac; using Microsoft.AspNet.Abstractions; -using Microsoft.AspNet.ConfigurationModel; -using Microsoft.AspNet.DependencyInjection; +using Microsoft.AspNet.DependencyInjection.Autofac; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Routing; -using Microsoft.Net.Runtime; namespace MvcSample.Web { public class Startup { - public void Configuration(IBuilder app) + public void Configuration(IBuilder builder) { - var configuration = new Configuration(); - var services = MvcServices.GetDefaultServices(configuration); - var serviceProvider = - DefaultServiceProvider.Create(app.ServiceProvider, services); + var containerBuilder = new ContainerBuilder(); + var services = MvcServices.GetDefaultServices(); + + AutofacRegistration.Populate(containerBuilder, builder.ServiceProvider, services); + containerBuilder.RegisterInstance(new PassThroughAttribute()); + + var serviceProvider = containerBuilder.Build().Resolve(); var routes = new RouteCollection() { @@ -30,7 +33,8 @@ namespace MvcSample.Web "{controller}", new { controller = "Home" }); - app.UseRouter(routes); + builder.UseRouter(routes); } } } +#endif diff --git a/samples/MvcSample.Web/ViewMetadata.cs b/samples/MvcSample.Web/ViewMetadata.cs new file mode 100644 index 0000000000..dc11624a2b --- /dev/null +++ b/samples/MvcSample.Web/ViewMetadata.cs @@ -0,0 +1,25 @@ +#if VIEWMETADATA +using System; +using System.Collections.Generic; + +public class ViewMetadata +{ + public static Dictionary Metadata + { + get + { + return new Dictionary + { + { + "~/Views/Home/MyView.cshtml", + typeof(MvcSample.Views.MyView) + }, + { + "~/Views/Shared/_Layout.cshtml", + typeof(MvcSample.Views.Layout) + } + }; + } + } +} +#endif \ No newline at end of file diff --git a/samples/MvcSample.Web/Views/Link/Details.cshtml b/samples/MvcSample.Web/Views/Link/Details.cshtml new file mode 100644 index 0000000000..f627dfdcba --- /dev/null +++ b/samples/MvcSample.Web/Views/Link/Details.cshtml @@ -0,0 +1,3 @@ + + +@Url.Action("About") diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml new file mode 100644 index 0000000000..3f8a25f030 --- /dev/null +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -0,0 +1,36 @@ +@using MvcSample.Web.Models +@model User +@{ + Layout = "/Views/Shared/_Layout.cshtml"; + ViewBag.Title = "Home Page"; + string nullValue = null; +} + +
+

ASP.NET

+

ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

+

Learn more »

+
+
+

Hello @Model.Name! Happy @Model.Age birthday.

+ +
+

Getting started

+

+ ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that + enables a clean separation of concerns and gives you full control over markup + for enjoyable, agile development. +

+

Learn more »

+
+
+

Get more libraries

+

NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.

+

Learn more »

+
+
+

Web Hosting

+

You can easily find a web hosting company that offers the right mix of features and price for your applications.

+

Learn more »

+
+
diff --git a/samples/MvcSample.Web/Views/Shared/_Layout.cshtml b/samples/MvcSample.Web/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000000..1f7c228ce3 --- /dev/null +++ b/samples/MvcSample.Web/Views/Shared/_Layout.cshtml @@ -0,0 +1,37 @@ + + + + + + @ViewBag.Title - My ASP.NET Application + + + + +
+ @RenderBody() +
+
+ @Model.Address +
+ +
+ + diff --git a/samples/MvcSample.Web/project.json b/samples/MvcSample.Web/project.json index c0ab33e38e..547ab36214 100644 --- a/samples/MvcSample.Web/project.json +++ b/samples/MvcSample.Web/project.json @@ -6,17 +6,31 @@ "Microsoft.AspNet.ConfigurationModel" : "0.1-alpha-*", "Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*", "Microsoft.AspNet.Routing" : "0.1-alpha-*", - "Microsoft.AspNet.Mvc.Core" : "", - "Microsoft.AspNet.Mvc" : "", - "Microsoft.AspNet.Mvc.Razor" : "" + "Microsoft.AspNet.Mvc.ModelBinding": "", + "Microsoft.AspNet.Mvc.Core": "", + "Microsoft.AspNet.Mvc": "", + "Microsoft.AspNet.Mvc.Razor": "", + "Microsoft.AspNet.Mvc.Rendering": "" }, "configurations": { - "net45": { }, + "net45": { + "dependencies": { + "Autofac": "3.3.0", + "Microsoft.AspNet.DependencyInjection.Autofac": "0.1-alpha-*", + "System.ComponentModel.DataAnnotations": "" + } + }, "k10" : { "dependencies": { - "System.Collections" : "4.0.0.0", - "System.Runtime" : "4.0.20.0", - "System.ComponentModel": "4.0.0.0" + "System.ComponentModel": "4.0.0.0", + "System.Console": "4.0.0.0", + "System.Diagnostics.Debug": "4.0.10.0", + "System.Diagnostics.Tools": "4.0.0.0", + "System.Dynamic.Runtime": "4.0.0.0", + "System.Runtime": "4.0.20.0", + "System.Runtime.InteropServices": "4.0.10.0", + "System.Threading.Tasks": "4.0.0.0", + "Microsoft.ComponentModel.DataAnnotations": "0.1-alpha-*" } } }