I'm porting my old ASP.NET Webform project to ASP.NET Core MVC. Due to various reasons, I need to keep the original code as much as possible to save manpower and material resources.
Then, in oruginal code, there's a IsPostBack property on every page. When the page is initializing, it will call a 'InitPage()' method. If the request is postback, it will not call the 'InitPage()' method again, it will call 'DoSomething()' method instead.
But now, I face such a issue. When the view is rendered at first time, this method worked well. We assume that all 'Index.cshtml' views are the initial views of all controllers.
For example, we have a 'HomeController', and there're 'Index' and 'Search' actions in it. They all accept 'GET' request.
When we access the 'http://localhost:xxx/Home/Index', the 'IsPostBack()' worked as we expected. When we access 'Search', the 'IsPostBack()' didn't work. It still call 'InitPage()' method for every request. I can understand this behavior, it's due to the url is changed and it's 'GET' request. But even if we make the 'Search' action accept 'POST' request. It still call 'InitPage()' method at first request.
So, my main target is hold the fixed properties value in ViewModel after 'InitPage()' method is called. Then, I do not need to call 'InitPage()' method every time in other action methods of the same controller.
Please give me some suggestions and make me on the right direction.
The following is the code:
public class HomeController : BasePage
{
private HomeViewModel viewModel;
public HomeController()
{
viewModel = new HomeViewModel();
}
public override void PageLoaded()
{
if (!IsPostBack())
{
this.InitPage();
}
else
{
// Do something.
this.viewModel.LabelName = this.HttpContext.Session.Get("Name").ToString();
}
}
public IActionResult Index()
{
return View(this.viewModel);
}
public IActionResult Search()
{
Random rn = new Random();
this.viewModel.Content = "rn : " + rn.Next(100);
return View("Index", this.viewModel);
}
private void InitPage()
{
this.viewModel.LabelName = "Steven -- " + DateTime.Now.ToString();
this.HttpContext.Session.Set("Name", Encoding.Default.GetBytes("this.viewModel.LabelName "));
}
}
public class BasePage : Controller
{
public virtual void PageLoaded() { }
public override void OnActionExecuting(ActionExecutingContext context)
{
this.PageLoaded();
base.OnActionExecuting(context);
}
protected bool IsPostBack()
{
var request = HttpContext.Request;
var currentUrl = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
var referrer = request.Headers["Referer"].FirstOrDefault();
bool isPost = string.Compare(request.Method, "POST",
StringComparison.CurrentCultureIgnoreCase) == 0;
if (referrer == null) return false;
bool isSameUrl = string.Compare(currentUrl,
referrer,
StringComparison.CurrentCultureIgnoreCase) == 0;
return isPost && isSameUrl;
}
}
public class HomeViewModel
{
public string LabelName { get; set; }
public string Content { get; set; }
}
@{
ViewData["Title"] = "Home Page";
}
@model WebApplication7.Controllers.HomeViewModel
<div class="text-center">
<form asp-action="Search" method="get">
<label>@Model.LabelName</label><br/>
<label>@Model.Content</label><br/>
<input type="submit" value="Search"/>
</form>
</div>