성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Get Started with Milvus Vector DB i...
[정성태] cyberark/PipeViewer - A tool that...
[정성태] WinForms in a 64-Bit world – our st...
[정성태] 예제에서 SELECT_SQL도 내부적으로는 SqlCommand/...
[victor] SELECT_LINQ SELECT_SQL 같은 쿼리인...
[victor] 답변 갑사합니다. 예외(Exception)가 났습니다. ...
[정성태] 일단, 위의 방식대로 하면 예외(Exception) 없이 잘 동...
[정성태] Windows 10 (버전 1809)에 이런 기능이 ^^ 추가되...
[정성태] pde windbg extension ; https://lea...
[정성태] // GetEnumerator extensions for Ran...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>ASP.NET의 HttpContext.Current 구현에 대응하는 ASP.NET Core의 IHttpContextAccessor/HttpContextAccessor 사용법</h1> <p> HttpContext.Current와 같은 구현이 왜 필요할까요?<br /> <br /> 가령 ASP.NET Core 프로젝트에서 별도의 라이브러리(DLL)를 참조해 현재 요청의 HOST 명을 반환하는 메서드를 구현한다고 가정해 보겠습니다. 만약 <a target='tab' href='https://www.sysnet.pe.kr/2/0/1608'>HttpContext.Current</a>가 없다면 ASP.NET Core 측에서 명시적인 Request 객체를 다음과 같이 넘겨줘야만 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // asp.net core 측에서 호출할 때, [HttpGet] public IEnumerable<string> Get() { ClassLibrary1.Class1 cl = new ClassLibrary1.Class1(); return new string[] { cl.<span style='color: blue; font-weight: bold'>GetHost(this.Request)</span> }; } </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // 라이브러리 프로젝트에서 Request를 접근할 때, using Microsoft.AspNetCore.Http; using System; namespace ClassLibrary1 { public class Class1 { public string GetHost(<span style='color: blue; font-weight: bold'>HttpRequest req</span>) { return <span style='color: blue; font-weight: bold'>req.Host</span>.ToString(); } } } </pre> <br /> 즉, 라이브러리에서 HttpRequest나 HttpResponse와 연관된 기능이 필요하다면 언제나 저렇게 명시적으로 객체를 넘겨줘야만 하는 것입니다.<br /> <br /> 그런데 이런 불편함을 기존 ASP.NET에서는 HttpContext.Current 객체를 이용해 해결했었습니다. 그리고 ASP.NET Core에서도 이와 유사한 역할을 하는 IHttpContextAccessor/HttpContextAccessor 타입을 제공해 주고 있는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > The IHttpContextAccessor service is not registered by default ; <a target='tab' href='https://github.com/aspnet/Announcements/issues/190'>https://github.com/aspnet/Announcements/issues/190</a> How to get HttpContext.Current in ASP.NET Core? [duplicate] ; <a target='tab' href='https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core'>https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core</a> </pre> <br /> 사용을 위해 ConfigureServices에서 다음과 같이 추가해 줘야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public void ConfigureServices(IServiceCollection services) { services.AddMvc(); <span style='color: blue; font-weight: bold'>services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();</span> } </pre> <br /> 이것을 사용하면 이전에 구현한 GetHost 메서드를 다음과 같이 바꿀 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Microsoft.AspNetCore.Http; using System; namespace ClassLibrary1 { public class Class1 { public string <span style='color: blue; font-weight: bold'>GetHost()</span> { <span style='color: blue; font-weight: bold'>HttpContextAccessor hca = new HttpContextAccessor();</span> return <span style='color: blue; font-weight: bold'>hca.HttpContext.Request.Host</span>.ToString(); } } } </pre> <br /> <a target='tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1224&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2389
(왼쪽의 숫자를 입력해야 합니다.)