網站搬遷
Hello all, 許久沒有寫部落格的習慣,未來會持續地在 https://alanzhan.dev/ 更新
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}" ); }); } }
routes.MapRoute( name: "default_route", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" } );
app.UseMvc(routes => { //New Route routes.MapRoute( name: "about-route", template: "about", defaults: new { controller = "Home", action = "About" } ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}" ); });原本的路由設定還是存在的,
[Route("[controller]")]
public class AnalyticsController : Controller
{
[Route("Dashboard")]
public IActionResult Index()
{
return View();
}
[Route("[action]")]
public IActionResult Charts()
{
return View();
}
}
我們可以這樣訪問控制器↓↓↓[Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable當然我們可以利用HttpGet, HttpPost, HttpPut, HttpDelete屬性!Get() { return new string[] {"Hello", "World!"}; } // POST api/values [HttpPost] public void PostCreate([FromBody] string value) { } }
[HttpGet("{id:int}")] public string GetById(int id) { return "item " + id; }上面的「{id:int}」可以改成「{id:int?}」
routes.MapRoute( name: "getProductById", template: "Products/{id:int}", defaults: new { controller = "Products", action = "GetById" } );
留言
張貼留言