在C#Controller中检测到无法访问的代码

时间:2014-07-18 14:01:00

标签: c# asp.net-mvc docx

您好我的控制器中有一些代码存在问题。我希望能够使用此代码在服务器上保存生成的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);
    }

2 个答案:

答案 0 :(得分:2)

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语句,将无法访问您的代码。它是如果其他,它们会返回......

答案 1 :(得分:1)

如果您按照所有不同的潜在分支进行操作,那么在进入此行之前,您会发现您在每个案例中都遇到return

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

您必须重新考虑if / else / return逻辑。