Razor보기 페이지에서 네임 스페이스를 가져 오려면 어떻게하나요?
Razor보기 페이지에서 네임 스페이스를 가져 오는 방법은 무엇입니까?
마침내 답을 찾았습니다.
@using MyNamespace
VB.Net의 경우 :
@Imports Mynamespace
앱 전체에 네임 스페이스를 포함하려면 @Javad_Amiry의 답변을 살펴보십시오.
첫 번째 방법은 파일에 있는 use @using
문 .cshtml
, 현재 파일에만 네임 스페이스를 가져 오는 방법입니다. 두 번째 방법은 다음 과 같습니다.
Views
프로젝트의 " "디렉토리에 있는 "web.config"파일 ( 프로젝트 루트의 기본 web.config 가 아님 ) 에서 다음 섹션을 찾으십시오.
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
.
.
<!-- etc -->
</namespaces>
</pages>
</system.web.webPages.razor>
다음과 같이 사용자 정의 네임 스페이스를 추가 할 수 있습니다.
<add namespace="My.Custom" />
그러면 모든 .cshtml (및 / 또는 .vbhtml) 파일에 네임 스페이스가 추가됩니다. 또한 다음과 같이 여기에서 뷰 상속을 변경할 수 있습니다.
<pages pageBaseType="My.Custom.MyWebViewPage">
문안 인사.
업데이트 : @ Nick Silberstein
지역에 대한 알림에 감사드립니다 ! 그는 말했다 :
당신이 내에서 작업하는 경우 지역 , 당신은 추가해야합니다 namespace
내 Web.config
에서 /Areas/<AreaName>/Views/
보다는/Views/
도서관 용
@using MyNamespace
모델
@model MyModel
ASP.NET MVC 3 Preview1에서는 Global.asax.cs의이 코드를 사용하여 모든 면도기 뷰에서 네임 스페이스를 가져올 수 있습니다.
Microsoft.WebPages.Compilation.CodeGeneratorSettings.AddGlobalImport("Namespace.Namespace");
RTM에서 이것이 Web.config 섹션을 통해 이루어지기를 바랍니다.
모든 면도기 페이지에 사용자 지정 네임 스페이스를 추가하는 방법을 설명하는 http://weblogs.asp.net/mikaelsoderstrom/archive/2010/07/30/add-namespaces-with-razor.aspx 를 찾았습니다 .
기본적으로 이것을 만들 수 있습니다
using Microsoft.WebPages.Compilation;
public class PreApplicationStart
{
public static void InitializeApplication()
{
CodeGeneratorSettings.AddGlobalImport("Custom.Namespace");
}
}
AssemblyInfo.cs에 다음 코드를 넣으십시오.
[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")]
InitializeApplication 메서드는 global.asax에서 Application_Start 전에 실행됩니다.
One issue that you must know is that when you import a namespace via web.config
in Views
folder, that namespace
is imported JUST for views in that folder. Means if you want to import a namespace
in an area views, you must also import that namespace
, in that area's web.config
file, located in area's Views
folder;
You can try this
@using MyNamespace
For namespace and Library
@using NameSpace_Name
For Model
@model Application_Name.Models.Model_Name
For Iterate the list on Razor Page (You Have to use foreach loop for access the list items)
@model List<Application_Name.Models.Model_Name>
@foreach (var item in Model)
{
<tr>
<td>@item.srno</td>
<td>@item.name</td>
</tr>
}
"using MyNamespace" works in MVC3 RTM. Hope this helps.
I think in order import namespace in razor view, you just need to add below way:
@using XX.YY.ZZ
Depending on your need you can use one of following method:
- In first line/s of view add "using your.domainName;" (if it is required in specific view only)
if required in all subsequent views then add "using your.domainName;" in _ViewStart.cshtml. You can find more about this in: Where and how is the _ViewStart.cshtml layout file linked?
Or add Assembly reference in View web.config as described by others explained in: How do you implement a @using across all Views in Asp.Net MVC 3?
참고URL : https://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page
'Programing' 카테고리의 다른 글
펜촉이 장착되었지만 '보기'콘센트가 설정되지 않았습니다. (0) | 2020.09.30 |
---|---|
#! (0) | 2020.09.30 |
파이썬에서 do-while 루프를 에뮬레이트합니까? (0) | 2020.09.30 |
Git에서 유효하지 않은 원격 분기 참조를 어떻게 제거합니까? (0) | 2020.09.30 |
JavaScript 배열을 선언 할 때“Array ()”와“[]”의 차이점은 무엇입니까? (0) | 2020.09.29 |