【问题标题】:How to get url in Layout view using ASP.net Core?如何使用 ASP.net Core 在布局视图中获取 url?
【发布时间】:2016-10-03 21:17:40
【问题描述】:

我需要将MVC 项目迁移到.net Core,我知道System.Web 已从ASP.net Core 中删除,我需要获取当前url 并根据它的值显示相同layout 但有一些变化。

这是我在 MVC 中的代码,我需要翻译成 ASP.net Core:

 public class LIGCalculators
    {

        public static bool LargePano(Uri url)
        {

            var path = url.AbsolutePath.ToLower();
            if (path == "/")
            {
                return true;
            }
            if (path.Contains("Home"))
            {
                return true;
            }
            return false;
        }
    }

Layout 视图(MVC),Request.UrlCore 中不起作用:

  @if (LIGCalculators.LargePano(Request.Url))
    {
        <div class="blue"></div>
        <div class="pano-bg"></div>
        <div class="bgs">
            <div class="orangeL"></div>
            <div class="blue2L"></div>
        </div>
    }
    else
    {
        <div class="blue-big"></div>
        <div class="small-pano-bg"></div>
        <div class="bgs">
            <div class="orange"></div>
            <div class="blue2"></div>
        </div>
    }

这在 ASP.net Core 中没有显示错误,但是如何从布局中调用此方法?如何将url作为参数发送?:

 public static bool LargePano(Uri url)
        {
           // return true;
            var path = url.AbsolutePath.ToLower();
            if (path == "/")
            {
                return true;
            }
            if (path.Contains("Home"))
            {
                return true;
            }
            return false;
        }

【问题讨论】:

    标签: c# asp.net-mvc razor asp.net-core


    【解决方案1】:

    我是用这段代码做到的:

     @{ 
               Uri url = new Uri(String.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString));           
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      相关资源
      最近更新 更多