如何在ActionResult Entity Framework Web API中调用存储过程?

时间:2017-06-09 08:41:55

标签: c# entity-framework asp.net-web-api

我正在尝试在ActionResult中执行存储过程以将其作为json格式返回。我的存储过程名称是db.SpCategoriesRead,我需要传递这些参数:(" 1.1",0,0,"")

我是Entity Framework的新手,我是使用直接命令完成的,但是我用存储过程替换它却无法正常工作。

这是我的代码:

public ActionResult cats()
{
        bool isValid = false;
        string authToken = "";

        if (Request["dt"] != null)
            authToken = Request["dt"].ToString().Trim();
        else
            return Content("{\"error\":\"Please send device token parameter 'dt'.\"}", "application/json");

        string message = (new CommonFunction()).ValidateToken(authToken, out isValid);

        if (isValid)
        {
            var q = db.SpCategoriesRead("1.1", 0, 0, "");

            //----------------------------------------------------------------------------------------------------------------------------------------
            //var q = db.Categories.Where(p => !p.ParentCategoryID.HasValue && p.StatusID.Equals(1));
            if (q.Count() < 1)
                return Content("{\"error\":\"No results found.\"}", "application/json");

            return Json(q.OrderBy(cat => cat.SortOrder).Select(c => new { c.CategoryID, c.CatName, c.IconFile, LogoFile = string.Concat("white-", c.LogoFile) }).ToList(), JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Content("{\"error\":\"" + message + "\"}", "application/json");
        }
}

0 个答案:

没有答案