ASP.NET MVC works fine on local machine but once deployed on Azure App Service, the pages related to a writing operation on the database do not display (pages that add data to the database and pages that update data in the database).
Here’s an exmple of code of one controller that inserts data:
public ActionResult Create()
{
ViewBag.nbEnt = db.Entrepots.Where(e => e.active == true).Count();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,entrName,entrAdres,LocaliteId,entrFreePlace,entrBusyPlace,entrTotalPlace,active")] Entrepot entrepot)
{
if (ModelState.IsValid)
{
entrepot.active = true;
db.Entrepots.Add(entrepot);
db.SaveChanges();
TempData["action"] = "add";
return RedirectToAction("Index");
}
ViewBag.LocaliteId = new SelectList(db.Localites, "Id", "locTown", entrepot.LocaliteId);
return View(entrepot);
}
Here’s an example of an update:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Entrepot entrepot = db.Entrepots.Find(id);
if (entrepot == null)
{
return HttpNotFound();
}
return View(entrepot);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,entrName,entrAdres,LocaliteId,entrFreePlace,entrBusyPlace,entrTotalPlace,active")] Entrepot entrepot)
{
if (ModelState.IsValid)
{
db.Entry(entrepot).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.LocaliteId = new SelectList(db.Localites, "Id", "locTown", entrepot.LocaliteId);
return View(entrepot);
}
Any clue or advice on this?
Thanks.
I’ve tried the debugger linked to Visual Studio but that hasn’t helped me.
2
Answers
I managed to solve the problem by removing from the View the anti forgery token
@Html.AntiForgeryToken()
and also removed it from the controller :
now the code works fine, thanks to you all for help and support, that helped me to test my code with your advices.
I tried the simple ASP. NET MVC Framework 4.8 with SQL Server and deployed it to Azure.
Ensure that your database schema is properly migrated when deploying to Azure. If you are using Entity Framework Code First migrations, make sure that your migrations are applied correctly to the Azure SQL Database.
Make Sure that your Azure SQL Database is properly configured and accessible from your Azure App Service.
This is my controller, used to create and update the data in my database.
EntrepotsController:
Below is my Azure SQL Server connection string.
Web.Config:
Local Output:
Before deploying a web app to Azure, if you are generating migrations using a local SQL Database ConnectionString, remove those migrations and generate new migrations using an Azure SQL Database ConnectionString.
Azure App Service Output:
Below Is My Azure Sql Database Output: