C# 如果在不更改类类型的情况下发生错误,则在int类内重定向

C# 如果在不更改类类型的情况下发生错误,则在int类内重定向,c#,redirect,asp.net-mvc-5,controller,C#,Redirect,Asp.net Mvc 5,Controller,我是c#的新手,在尝试设置特定类的捕获时遇到了问题 我的代码中已经有很多trycatch和重定向,但我似乎无法得到以下部分,我知道问题是因为类是一个int,应该是一个ActionResult来使用重定向,但是我在返回int时得到了一个错误。我需要这个类来处理这两个问题 BaseController中当前有效的类,我在大多数其他控制器上调用GetUID() public int GetUID() { return int.Parse(System.Web.HttpC

我是c#的新手,在尝试设置特定类的捕获时遇到了问题

我的代码中已经有很多trycatch和重定向,但我似乎无法得到以下部分,我知道问题是因为类是一个int,应该是一个ActionResult来使用重定向,但是我在返回int时得到了一个错误。我需要这个类来处理这两个问题

BaseController中当前有效的类,我在大多数其他控制器上调用GetUID()

    public int GetUID()
    {
        return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
    }
我的问题是,如果有人的会话超时,他们将得到一个错误,但我希望他们被重定向到登录页面

所以我试着做下面的事情

    public int GetUID()
    {
        try
        {
            return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
        }
        catch(Exception)
        {
            return RedirectToAction("Login", "Account");
        }
    }
    public ActionResult GetUID()
    {
        try
        {
            return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
        }
        catch(Exception)
        {
            return RedirectToAction("Login", "Account");
        }
    }
由于该类是一个int类,我在重定向时遇到一个错误:“无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'int'

我也试着做到以下几点:

    public int GetUID()
    {
        try
        {
            return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
        }
        catch(Exception)
        {
            return RedirectToAction("Login", "Account");
        }
    }
    public ActionResult GetUID()
    {
        try
        {
            return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
        }
        catch(Exception)
        {
            return RedirectToAction("Login", "Account");
        }
    }
但我得到了相反的错误:“无法将类型'int'隐式转换为'System.Web.Mvc.RedirectToRouteResult'”

有没有一种方法可以让类返回int,但如果重定向到登录页面时出错? 我看过SO和Google,我可以看到很多关于从视图转换为操作的查询,但没有类似的。
感谢您的帮助。

您必须定义并返回一个描述重定向操作的
int
错误代码,当您看到它时,您必须调用
redirectoaction
方法

编辑: 不能返回不同的类型。然后或返回一个对象:

public object GetUID(){...}
在BaseController中,您可以执行以下操作:

object o = GetUID();
if (o is int UID)
    //do your stuff 
else if (o is ActionResult action)
    //execute the action
int uid = GetUID();
switch(uid)
{
    case ReturnHomeError:
        //Call RedirectToAction("Login", "Account")
        break;
    default:
        //Do your stuff
        break;
}
另一种方法是定义错误代码,例如负数是错误。然后您将有:

public const int ReturnHomeError = -1;
public int GetUID()
{
    try
    {
        return int.Parse(System.Web.HttpContext.Current.Session["UID"].ToString());
    }
    catch(Exception)
    {
        return ReturnHomeError;
    }
}
在BaseController中,您可以执行以下操作:

object o = GetUID();
if (o is int UID)
    //do your stuff 
else if (o is ActionResult action)
    //execute the action
int uid = GetUID();
switch(uid)
{
    case ReturnHomeError:
        //Call RedirectToAction("Login", "Account")
        break;
    default:
        //Do your stuff
        break;
}

谢谢你的回复,因为我说我是c#新手,所以我不确定这意味着什么,你能给我举个例子,或者指导我到一个更详细地描述这一点的网页吗?我不知道当我用谷歌搜索我的问题时该用什么措辞,谢谢这个版本足够让我上路了,谢谢。