C# 获取错误:并非所有代码路径都返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10731097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:02:05  来源:igfitidea点击:

Getting Error: Not all code paths return a value

c#

提问by user1382770

I am new to mvc C# and am stuck. Please advise on how to fix this. I'm getting the error on Add. When I hover over the red squiggly line it says "Not all code paths return a value"

我是 mvc C# 的新手并且被卡住了。请告知如何解决此问题。我在添加时遇到错误。当我将鼠标悬停在红色波浪线上时,它会显示“并非所有代码路径都返回一个值”

    public ActionResult Add(ShapeInputModel dto, FormCollection collection)
    {

        var model = new GeoRegions();

        if (TryUpdateModel(model))
        {


            var destinationFolder = Server.MapPath("/App_Data/KML");
            var postedFile = dto.Shape;

            if (postedFile != null)
            {
                var fileName = Path.GetFileName(postedFile.FileName);
                var path = Path.Combine(destinationFolder, fileName);
                postedFile.SaveAs(path);

                //Save to Database
                Db.AddGeoRegions(model);
                return RedirectToAction("Index");

            }

            return View();

        }
    }

采纳答案by Writwick

Use This :

用这个 :

public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{
    var model = new GeoRegions();

    if (TryUpdateModel(model))
    {
        var destinationFolder = Server.MapPath("/App_Data/KML");
        var postedFile = dto.Shape;

        if (postedFile != null)
        {
            var fileName = Path.GetFileName(postedFile.FileName);
            var path = Path.Combine(destinationFolder, fileName);
            postedFile.SaveAs(path);

            //Save to Database
            Db.AddGeoRegions(model);
            return RedirectToAction("Index");
        }
        return View();

    }
    return null; // you can change the null to anything else also.
}

The error happens because your functions doesn't return anything if TryUpdateModel(model) = false. So adding the line return nullor return 'any other thing'will solve the problem!

发生错误是因为您的函数不返回任何内容 if TryUpdateModel(model) = false。所以加行return null还是return 'any other thing'会解决问题!

回答by user1382770

There is no returnif the "if" is never entered.

没有return如果“如果”永远不会进入。

I like to always keep if-else's used [with return] balanced to I can see the return values (and that all paths have a return value) at a glance:

我喜欢始终保持 if-else 使用的 [with return] 平衡,以便我可以一目了然地看到返回值(并且所有路径都有返回值):

if (TryUpdateModel(model))
{
    ...
    if (postedFile != null)
    {
        ...
        return RedirectToAction("Index");
    } else {
        return View();
    }
} else {
    return View(); // or null/whatever is appropriate
}

Of course ReSharper often tells me I have "useless" else statements ;-)

当然,ReSharper 经常告诉我我有“无用”的 else 语句;-)

Happy coding.

快乐编码。

回答by Andrew Barber

Nothing is being returned if (TryUpdateModel(model))returns false. Maybe you meant to have your return View();outside of the if?

如果(TryUpdateModel(model))返回,则不会返回任何内容false。也许你的意思是让你的return View();外面if

回答by Adam Robinson

The error does what it says on the tin; there's a code path where the function will complete but will not return a value. A function that has a return type must always either return a value or throw an exception.

错误按照它在锡上说的那样做;有一个函数将完成但不会返回值的代码路径。具有返回类型的函数必须始终返回值或抛出异常。

In your case, if TryUpdateModel(model)returns false, you have no return value.

在您的情况下,如果TryUpdateModel(model)返回false,则没有返回值。

回答by Richard J. Ross III

Well read the error! At some point in your method's execution, you must return a value, or throw an exception. (I think that returning null is in order in this case)

阅读错误!在方法执行的某个时刻,您必须返回一个值,或者抛出一个异常。(我认为在这种情况下返回 null 是正确的)

回答by Icarus

Certainly, your initial if (TryUpdateModel(model))makes your routine only return a value when the condition is true; if it isn't, nothing will be returned, which violates the method signature.

当然,您的初始值if (TryUpdateModel(model))使您的例程仅在条件为true;时返回一个值;如果不是,则不会返回任何内容,这违反了方法签名。

回答by Kirk Broadhurst

You have

你有

if (TryUpdateModel(model))
{
    // lots of stuff

    return View();
}

So what will be returned if TryUpdateModelis not true?

那么如果TryUpdateModel不是真的会返回什么?

Your method must return an ActionResulteven when the ifstatement is false.

ActionResult即使if语句为假,您的方法也必须返回一个。

回答by Abhi.Net

Just having a quick look at your code I can see that you have your return command(return View() inside the "if" statement block. Now if the "If" condition was to fail there is no return statement outside it's scope. The easiest way would be to

只需快速查看您的代码,我就可以看到您的 return 命令(return View() 在“if”语句块内。现在,如果“If”条件失败,则在其范围之外没有 return 语句。最简单的方法是

 }

            return View();

        }
return null; // Depends upon your code though. you might want to return something else
}

回答by Tilak

Add

添加

 return null;

before the last line }

在最后一行之前 }

回答by Syed Yunus

You are returning value inside "if()" condition. What suppose when if condition fails?? the program will wont return value. So, return any default value outside if condition, it may be in else condition.

您正在“if()”条件内返回值。假设条件失败时怎么办?该程序将不会返回值。因此,在 if 条件之外返回任何默认值,它可能在 else 条件下。

public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{

    var model = new GeoRegions();

    if (TryUpdateModel(model))
    {
     ....
     ....
    }
    return default_value;//it may be in else condition also.
}

Try it. And if ur problem is solved then mark as answered. So that it is useful to others.

尝试一下。如果您的问题得到解决,则标记​​为已回答。以便对其他人有用。