locked
Void Function Not Working In ASP.net Core RRS feed

  • Question

  • User2041008840 posted

    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();
                }
            }



    Wednesday, July 3, 2019 5:29 PM

Answers

All replies

  • User475983607 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 5:38 PM
  • User2041008840 posted

    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();

    Wednesday, July 3, 2019 5:43 PM
  • User475983607 posted

    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.

    Wednesday, July 3, 2019 5:49 PM