Hi, Create Small func. just to check session is active or not. After I clicked on details Button it shows error that MyID is null and Throw error at _context.savechangesasync()
but when my session is active it will not show any error and works file.
Session is not empty when i login. but if its not login then it should be redirect to login page after click on details button. but its not working.
I still not getting any solution on this please help me..
private int MyID = 0;
public void ActiveUser()
{
if (string.IsNullOrEmpty(HttpContext.Session.GetString("CustomerID")))
{
Response.Redirect("/home/signin");
}
else
{
MyID = Convert.ToInt32(HttpContext.Session.GetString("CustomerID"));
//MyID = (int)System.Web.HttpContext.Current.Session["CustomerID"];
}
}
[HttpGet]
// GET: Shops/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
Shop shop = await _context.Shops
.FirstOrDefaultAsync(m => m.ID == id);
ViewBag.ProductFormat = _context.ProductFormats.Include(a => a.Format).Where(a => a.ShopID == id);
if (shop == null)
{
return NotFound();
}
return View(shop);
}
[HttpPost]
public async Task<IActionResult> Details(IFormCollection fc)
{
ActiveUser(); //check while clicking on Details button
if (ModelState.IsValid)
{
_context.Carts.Add(new Cart
{
ShopID = Convert.ToInt32(fc["ShopID"]),
CustomerID = MyID
});
await _context.SaveChangesAsync();
return Redirect("/shops/buy/");
}
else
{
return View();
}
}
But If ActiveUser(); is == null then it should be redirect to login page (url)
but why its not working ?
but its moving to next line and checking CustomerID = MyID
and throw error at _saveChangesasync();
I already explained why the code does not work. Action method return HTTP responses this include a redirect. Your void method simply will not work as it does not follow MVC convention.
You need to rethink the design. I recommend using ASP.NET Core Identity and dropping the use of Session especially if you are new to building secure applications.
Member
28 Points
52 Posts
Void Function Not Working In ASP.net Core
Jul 03, 2019 05:29 PM|Prathamesh Shende|LINK
Hi, Create Small func. just to check session is active or not. After I clicked on details Button it shows error that MyID is null and Throw error at _context.savechangesasync()
but when my session is active it will not show any error and works file.
Session is not empty when i login. but if its not login then it should be redirect to login page after click on details button. but its not working.
I still not getting any solution on this please help me..
All-Star
43741 Points
18724 Posts
Re: Void Function Not Working In ASP.net Core
Jul 03, 2019 05:38 PM|mgebhard|LINK
You have a several design issues. In MVC a redirect is returned. Your method returns void.
You should use standard authentication patterns if you are not familiar with writing secured code. See ASP.NET Core Identity.
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.2&tabs=visual-studio
Member
28 Points
52 Posts
Re: Void Function Not Working In ASP.net Core
Jul 03, 2019 05:43 PM|Prathamesh Shende|LINK
But If ActiveUser(); is == null then it should be redirect to login page (url)
but why its not working ?
but its moving to next line and checking CustomerID = MyID
and throw error at _saveChangesasync();
All-Star
43741 Points
18724 Posts
Re: Void Function Not Working In ASP.net Core
Jul 03, 2019 05:49 PM|mgebhard|LINK
I already explained why the code does not work. Action method return HTTP responses this include a redirect. Your void method simply will not work as it does not follow MVC convention.
You need to rethink the design. I recommend using ASP.NET Core Identity and dropping the use of Session especially if you are new to building secure applications.