itsource

ASP.NET Core - 'JsonRequestBehavior' 이름이 현재 컨텍스트에 없습니다.

mycopycode 2023. 2. 10. 21:52
반응형

ASP.NET Core - 'JsonRequestBehavior' 이름이 현재 컨텍스트에 없습니다.

ASP에서.NET 코어(.NET Framework) 프로젝트, 다음 컨트롤러 액션 메서드에서 오류 이상의 오류가 발생하였습니다.내가 뭘 놓쳤을까?또는 어떤 작업이 있습니까?

public class ClientController : Controller
{
    public ActionResult CountryLookup()
    {
        var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };
        
        return Json(countries, JsonRequestBehavior.AllowGet);
    }
}

갱신:

@NateBarbettini의 코멘트는 다음과 같습니다.

  1. JsonRequestBehavior는 ASP에서 폐지되었습니다.NET Core 1.0.
  2. 아래 @Miguel에서 수락한 응답으로return type작용법의does not특히 JsonResult 유형이어야 합니다.Action Result 또는 IAction Result도 작동합니다.

Json 형식의 데이터 반환:

public class ClientController : Controller
{
    public JsonResult CountryLookup()
    {
         var countries = new List<SearchTypeAheadEntity>
         {
             new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
             new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
         };

         return Json(countries);
    }
}

코드에서는 대체 대상입니다.JsonRequestBehavior.AllowGet와 함께new Newtonsoft.Json.JsonSerializerSettings()

와 같은 작업입니다.JsonRequestBehavior.AllowGet

public class ClientController : Controller
{
  public ActionResult CountryLookup()
  {
    var countries = new List<SearchTypeAheadEntity>
        {
            new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
            new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
        };

    return Json(countries, new Newtonsoft.Json.JsonSerializerSettings());
  }
}

JSON에서 메시지를 반환해야 할 경우 다음과 같이 JSON 결과를 사용합니다. 더 이상 json request behavior를 수행할 필요가 없습니다.사용하기 쉬운 코드 아래에 있습니다.

public ActionResult DeleteSelected([FromBody]List<string> ids)
{
    try
    {
        if (ids != null && ids.Count > 0)
        {
            foreach (var id in ids)
            {
                bool done = new tblCodesVM().Delete(Convert.ToInt32(id));
                
            }
            return Json(new { success = true, responseText = "Deleted Scussefully" });

        }
        return Json(new { success = false, responseText = "Nothing Selected" });
    }
    catch (Exception dex)
    {
        
        return Json(new { success = false, responseText = dex.Message });
    }
}

컨트롤러에서 수락된 답변으로 안녕하세요. 당신은 말할 필요가 없습니다.

return Json(countries, JsonRequestBehavior.AllowGet);

그냥 쓰다

return Json(countries);

단, ajax의 cshtml에서는 shortCode와 name과 같이 소문자로 시작하는 엔티티 속성을 호출해야 합니다.

$.ajax({
                method: "GET",
                url: `/ClientController/CountryLookup`
            }).done(function (result) {
                for (var i = 0; i < result.length; i++) {
                        var shortCode=result[i].shortCode;
                        var name= result[i].name;
                }

            })

asp.net에서 asp.net Core로 웹사이트를 이식하고 있습니다.대체:return Json(data, JsonRequestBehavior.AllowGet);와 함께Json(data, new System.Text.Json.JsonSerializerOptions());그리고 모든 것이 다시 작동하기 시작했습니다.

언급URL : https://stackoverflow.com/questions/38578463/asp-net-core-the-name-jsonrequestbehavior-does-not-exist-in-the-current-cont

반응형