Asp.net mvc 是否在MVC4\U布局页面中导入外部标题(链接)?

Asp.net mvc 是否在MVC4\U布局页面中导入外部标题(链接)?,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我的公司有用php开发的通用头文件。我需要将该页面导入项目中的布局页面。标题可以称为“company.com/inc/custom/footer2sn/” 如何称呼它?您可以使用Html.RenderPartial: @{ Html.RenderPartial("SomeView"); } 但是,最好让您的布局按层次相互继承,并将HTML直接放在它所属的层中,用于常见布局元素: \u Layout.cshtml <!doctype html> <html> &l

我的公司有用php开发的通用头文件。我需要将该页面导入项目中的布局页面。标题可以称为“company.com/inc/custom/footer2sn/”


如何称呼它?

您可以使用
Html.RenderPartial

@{ Html.RenderPartial("SomeView"); }
但是,最好让您的布局按层次相互继承,并将HTML直接放在它所属的层中,用于常见布局元素:

\u Layout.cshtml

<!doctype html>
<html>
    <head>
        ...
    </head>
    <body>
        <header>
            ...
        </header>

        @RenderBody()

        <footer>
            ...
        </footer>
    </body>
</html>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

<div id="container">

    <div id="content">
        @RenderBody()
    </div>

    <aside id="sidebar">
        ...
    </aside>
</div>

...
...
@RenderBody()
...
\u TwoColumnLayout.cshtml

<!doctype html>
<html>
    <head>
        ...
    </head>
    <body>
        <header>
            ...
        </header>

        @RenderBody()

        <footer>
            ...
        </footer>
    </body>
</html>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

<div id="container">

    <div id="content">
        @RenderBody()
    </div>

    <aside id="sidebar">
        ...
    </aside>
</div>
@{Layout=“~/Views/Shared/_Layout.cshtml”}
@RenderBody()
...

您可以根据需要继续构建像这样的层。只需将布局设置为它应该从中继承的模板,然后将下一个子模板或视图的内容放置在
@RenderBody()
的位置。

您可以使用
Html.RenderPartial

@{ Html.RenderPartial("SomeView"); }
但是,最好让您的布局按层次相互继承,并将HTML直接放在它所属的层中,用于常见布局元素:

\u Layout.cshtml

<!doctype html>
<html>
    <head>
        ...
    </head>
    <body>
        <header>
            ...
        </header>

        @RenderBody()

        <footer>
            ...
        </footer>
    </body>
</html>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

<div id="container">

    <div id="content">
        @RenderBody()
    </div>

    <aside id="sidebar">
        ...
    </aside>
</div>

...
...
@RenderBody()
...
\u TwoColumnLayout.cshtml

<!doctype html>
<html>
    <head>
        ...
    </head>
    <body>
        <header>
            ...
        </header>

        @RenderBody()

        <footer>
            ...
        </footer>
    </body>
</html>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

<div id="container">

    <div id="content">
        @RenderBody()
    </div>

    <aside id="sidebar">
        ...
    </aside>
</div>
@{Layout=“~/Views/Shared/_Layout.cshtml”}
@RenderBody()
...

您可以根据需要继续构建像这样的层。只需将
布局设置为它应该继承的模板,然后将
@RenderBody()
放在下一个子模板或视图的内容应该放在的位置。

如果要包含的页面是静态HTML页面,则可以使用部分。 只需将somepage.html更改为somepage.cshtml

例如:

@Html.Partial("~/Path/to/somefile.cshtml")
尝试呈现普通HTML文件会出现错误,例如找不到页面或找不到呈现引擎

因此,如果您有一个静态HTML页面,请将扩展名更改为CSHTML并使用@HTML.Partial()

如果要包含的头文件是一个PHP文件,那么只要您有一个已启动并正在运行的服务器,并且准备好为PHP页面生成的HTML提供服务,就可以了

您可以编写自定义HTML帮助程序

public static class MyHelpers
{
  public static HtmlString RenderPHP(this HtmlHelper helper, string path)
  {
    var requestContext = helper.ViewContext.RequestContext;
    UrlHelper url = new UrlHelper(requestContext);
    var client = new WebClient();
    var returnString= client.DownloadString(new Uri(string.format("Http://{0}{1}",      requestContext.HttpContext.Request.Url.Host, url.Content(path))));
    return MvcHtmlString.Create(returnString);
  }
}

简而言之,这只需要获取从PHP页面生成的HTML,并将其注入页面中的一个部分

要在页面中使用此选项,请使用Razor语法,如下所示:

<div id="phpPage">
   @Html.RenderPHP("company.com/inc/custom/footer2sn/somepage.php"). <!-- Note this must on a server capable of rendering the php  -->

@renderhp(“company.com/inc/custom/footer2sn/somepage.php”)。

如果要包含的页面是静态HTML页面,则可以使用分部。 只需将somepage.html更改为somepage.cshtml

例如:

@Html.Partial("~/Path/to/somefile.cshtml")
尝试呈现普通HTML文件会出现错误,例如找不到页面或找不到呈现引擎

因此,如果您有一个静态HTML页面,请将扩展名更改为CSHTML并使用@HTML.Partial()

如果要包含的头文件是一个PHP文件,那么只要您有一个已启动并正在运行的服务器,并且准备好为PHP页面生成的HTML提供服务,就可以了

您可以编写自定义HTML帮助程序

public static class MyHelpers
{
  public static HtmlString RenderPHP(this HtmlHelper helper, string path)
  {
    var requestContext = helper.ViewContext.RequestContext;
    UrlHelper url = new UrlHelper(requestContext);
    var client = new WebClient();
    var returnString= client.DownloadString(new Uri(string.format("Http://{0}{1}",      requestContext.HttpContext.Request.Url.Host, url.Content(path))));
    return MvcHtmlString.Create(returnString);
  }
}

简而言之,这只需要获取从PHP页面生成的HTML,并将其注入页面中的一个部分

要在页面中使用此选项,请使用Razor语法,如下所示:

<div id="phpPage">
   @Html.RenderPHP("company.com/inc/custom/footer2sn/somepage.php"). <!-- Note this must on a server capable of rendering the php  -->

@renderhp(“company.com/inc/custom/footer2sn/somepage.php”)。

我无法将标题放入html。因为我们的webcontent团队保留了该标题的更改。我只有一个选择,需要调用我的页面。我不能告诉你们该做什么,但这个过程不应该指示糟糕的设计。为什么您的webcontent团队不能直接处理实际使用的布局?我不这么认为。因为其他一些web应用程序也使用相同的方式来包含该标题。我不能将标题放入html。因为我们的webcontent团队保留了该标题的更改。我只有一个选择,需要调用我的页面。我不能告诉你们该做什么,但这个过程不应该指示糟糕的设计。为什么您的webcontent团队不能直接处理实际使用的布局?我不这么认为。因为其他一些web应用程序也使用相同的方式,包括该标题。这正是我要寻找的工作?但标题代码呈现到我的页面中。它在代码中有相对路径,如
href=“/events/calendar/”
。因此,我的本地信息不是这些信息,因为它们来自外部。如何管理它?老实说,我不知道MVC将如何处理相对路径。如果我真的遇到了什么,我会让你知道这正是我正在寻找的工作?但标题代码呈现到我的页面中。它在代码中有相对路径,如
href=“/events/calendar/”
。因此,我的本地信息不是这些信息,因为它们来自外部。如何管理它?老实说,我不知道MVC将如何处理相对路径。如果我真的遇到什么事,我会告诉你的