Skip to content

Commit

Permalink
Setting up conventional routing + adding an AboutController with two …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
gorohoroh committed Jul 3, 2018
1 parent f6379b2 commit 3f4f74f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
15 changes: 15 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Controllers/AboutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace OdeToFoodRider.Controllers
{
public class AboutController
{
public string Phone()
{
return "1+555+555+5555";
}

public string Address()
{
return "USA";
}
}
}
14 changes: 11 additions & 3 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -31,14 +32,21 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter
}

app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.UseMvc(ConfigureRoutes);

app.Run(async (context) =>
{
var greeting = greeter.GetMessageOfTheDay();
// RDVS: wanted to use string interpolation for the WriteAsync() argument but a context action is missing :(
await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}");
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Not found");
});
}

private void ConfigureRoutes(IRouteBuilder routeBuilder)
{
// /Home/Index/4
routeBuilder.MapRoute("Default",
"{controller=Home}/{action=Index}/{id?}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OdeToFoodVisualStudio.Controllers
{
public class AboutController
{
public string Phone()
{
return "1+555+555+5555";
}

public string Address()
{
return "USA";
}

}
}
15 changes: 12 additions & 3 deletions OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter
}

app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.UseMvc(ConfigureRoutes);

app.Run(async (context) =>
{
Expand All @@ -42,10 +43,18 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter
// 3. Roslyn doesn't infer that the generated method should return a string, not an object
var greeting = greeter.GetMessageOfTheDay();
await context.Response.WriteAsync($"{greeting}: {env.EnvironmentName}");
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Not found");
});
}

private void ConfigureRoutes(IRouteBuilder routeBuilder)
{
// /Home/Index/4
routeBuilder.MapRoute("Default",
"{controller=Home}/{action=Index}/{id?}");

}
}

}

0 comments on commit 3f4f74f

Please sign in to comment.