question

SPIDEYJOE-3858 avatar image
0 Votes"
SPIDEYJOE-3858 asked ShuaiHuaDu edited

How to redirect to a specific controller from this function below ?

//Admin => model class name.

public Admin sess()
{
Admin dis = new Admin();

         var id = Session["userId"];

         if(id == null)
         {
             ViewBag.dis = "Not Logged in";

          //return redirect("log");
         }
         if(id != null)
         {
             ViewBag.dis = "Logged In";
         }

         return dis;
     }




dotnet-aspnet-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The syntax is....

 return RedirectToAction("ActionName")

https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2

However, it is not possible to return a redirect and an Admin. ViewBag is only good for the current request and will be lost after the redirect. Can you explain what you are trying to do?

This was explained in another of your threads.
https://docs.microsoft.com/en-us/answers/questions/867153/how-to-display-a-message-using-viewbag-in-the-belo.html



0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @SPIDEYJOE-3858,
From your description, I think you want to implement redirection to a specific controller in your model class.
This should be impossible.
The logic to determine if a redirect is required and what the redirect should be belongs to the controller. The model just gets the data the view needs. This happens after you decide which view to render. Think of redirects as instructions to perform different controller actions.
https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2#system-web-mvc-controller-redirecttoaction(system-string-system-string)
So, something like this:

 public ActionResult Foo()
 {
     var result = someOtherClass.Bar();
    
     if (result.WhatEver)
     {
         return RedirectToAction("yourAnotherActionName","yourAnotherControllerName");
     }
    
     return View(...);
 }

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RimRanshuijsen-2961 avatar image
0 Votes"
RimRanshuijsen-2961 answered

Of you are looking for a way to redirect Users to the login controller you are probabilistisch better off overriding the controller with your own and get the user from this controller and do the redirect there. This way all your controllers that require checks on access work the same way

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ShuaiHuaDu avatar image
0 Votes"
ShuaiHuaDu answered ShuaiHuaDu edited

You can not redirect to a controller from your model class.

You can redirect to controller with model data / route data in a Mvc controller.

e.g.:


 RedirectToAction("ActionName", "ControllerName", data);


If right, please Accept.
Enjoy Programming!!!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.