aspnetcore simple pseudo-static

Although completely static web page URL has the advantage of open fast, but great website content, then bound volume will become much larger site, there will be a lot of static documents, website migration, then a lot of trouble

On the other hand, if the content of the site a lot of time to modify the template, then again static time will be a relatively large amount of work. The actual use of the words still need to be selected according to their actual needs.

Before configuring routing information, pay attention to the order, pseudo-static routes to the default route

app.UseMvc(routes =>
{
    routes.MapRoute("Notice", "/Notice/{path}.html", new
    {
        controller = "Home",
        action = "NoticeDetails"
    });

    routes.MapRoute(name: "areaRoute",
        template: "{area:exists}/{controller=Home}/{action=Index}");

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}");
});

 

Controller:

 

/// <summary>
/// 公告详情
/// </summary>
/// <param name="path">访问路径</param>
/// <returns></returns>
public async Task<ActionResult> NoticeDetails(string path)
{
    if (string.IsNullOrWhiteSpace(path))
    {
        return RedirectToAction("Notice");
    }
    try
    {
        var noticeBll = HttpContext.RequestServices.GetService<IBLLNotice>();
        var notice = await noticeBll.FetchAsync(n => n.NoticeCustomPath == path.Trim());
        if (notice != null)
        {
            notice.NoticeVisitCount += 1;
            await noticeBll.UpdateAsync(notice, x => x.NoticeVisitCount);

            return View(notice);
        }
        else
        {
            return RedirectToAction("Notice");
        }
    }
    catch (Exception ex)
    {
        Logger.Error(ex);
        throw;
    }
}

actual effect:

https://reservation.weihanli.xyz/Notice/test-notice.html

Guess you like

Origin www.cnblogs.com/wwwan/p/11209945.html