在c#控制器中检测到不可达代码

本文关键字:代码 检测 控制器 | 更新日期: 2023-09-27 18:13:25

嗨,我有一个问题与一些代码在我的控制器。我想能够保存生成的Word文档与此代码的服务器上,但它给了我一个"无法到达的代码检测"错误。如有任何帮助,不胜感激。

[AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public ActionResult STAWordGen2(UserLogon model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.ApplicantID, model.UserPassword))
            {
                FormsService.SignIn(model.ApplicantID, model.RememberMe);
                if (Url.IsLocalUrl((returnUrl)))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Test");
                }
            }
            else
            {
                TempData["alert"] = "Login Failed.  The user name or password provided is incorrect.";
                return View();
            }
            string filename = HttpContext.Server.MapPath("~/Content/cvs/worddoc/test.docx");
            var doc = DocX.Create(filename);
            string headLine = "Test fingers crossed";
            doc.InsertParagraph(headLine);
            doc.Save();
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

在c#控制器中检测到不可达代码

string filename = HttpContext.Server.MapPath("~/Content/cvs/worddoc/test.docx");
var doc = DocX.Create(filename);
string headLine = "Test fingers crossed";
doc.InsertParagraph(headLine);
doc.Save();

在这部分代码之前,有if-else语句返回。由于else语句,无法到达您的代码。它要么是如果其他,他们返回…

如果您遵循所有不同的潜在分支,您会发现在到达这一行之前,您在每种情况下都遇到了return:

string filename = HttpContext.Server.MapPath("~/Content/cvs/worddoc/test.docx");

你必须重新考虑你的if/else/return逻辑