如果类是DLL

时间:2017-03-17 09:08:12

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

我使用的是asp.net mvc4。

我正在使用DLL,因此我必须覆盖类以扩展新方法。

我有这样的观点:

@model SalesAgentLoginInfoModel
<div class="top-dropdown login">
    <div class="opener collapsed">
        <span class="login-title">@Sana.SimpleText("Account")</span>
    </div>
    <div class="summary">
        <div class="welcome border-horizontal">
            @Sana.SimpleText("Login_Welcome")
            <span class="user-name">@Model.Name</span>
        </div>
        <a href="@Url.Sana.MyAccount()" class="hyp">@Sana.SimpleText("Login_MyAccount")</a>

        <a href="@Url.Sana.RepresentCustomer()" class="hyp">@Sana.SimpleText("ButtonText_RepresentCustomer")</a>

        <a href="@Url.Sana.RepresentCustomer()" class="hyp">@Sana.SimpleText("ButtonText_baliecode")</a>
        dlkfnslkdf
        <div class="logout border-horizontal">
            <a id="logoutLink" href="@Url.Sana.Logout()" class="arrow-blue">@Sana.SimpleText("Logout_LinkText")</a>
        </div>
    </div>
</div>

RepresentCustomer()来自这个类:

     // Summary:
        //     The web shop URLs constructor.
        public class UrlsBuilder
        {


 public virtual string RepresentCustomer(string returnUrl = null);
        //
        // Summary:
        //     Gets the URL to the reset password page.
        //
        // Returns:
        //     The reset password page URL.
            //
            // Summary:
            //     Initializes a new instance of the Sana.Commerce.Web.Routing.UrlsBuilder class.
            //
            // Parameters:
            //   routes:
            //     The route collection.
            public UrlsBuilder(RouteCollection routes);

            //
            // Summary:
            //     Gets the instance of Sana.Commerce.Web.Routing.UrlsBuilder.
            public static UrlsBuilder Current { get; }
            //
            // Summary:
            //     The routes used to construct URLs.
            public RouteCollection Routes { get; protected set; }

            //
            // Summary:
            //     Gets the URL to the add review page.
            //
            // Parameters:
            //   productId:
            //     The product ID.
            //
            //   returnUrl:
            //     The return URL.
            //
            //   navigationNodeId:
            //     The navigation node ID.
            //
            // Returns:
            //     The add review page URL.
            public virtual string AddReview(string productId, string returnUrl = null, string navigationNodeId = null);
    }

所以我用类似的新方法扩展了UrlsBuilder类:

 public class V_ExtendendURlsBuilder : UrlsBuilder
    {
        public V_ExtendendURlsBuilder(RouteCollection routes) : base(routes)
        {
        }

        public virtual string BaliecodeVerkoper( string url)
        {

            return url;

        }
}

但是如果我尝试在视图中调用这样的新方法:

 <a href="@Url.Sana...." class="hyp">@Sana.SimpleText("ButtonText_baliecode")

我没有看到新方法BaliecodeVerkoper。

还有一件事。 public UrlsBuilder Sana { get; }

在此课程内:

 public class FrontendUrlHelper : SanaUrlHelper
    {
        //
        // Summary:
        //     The facet values query string separator.
        public const string FacetValuesSeparator = "\n";

        //
        // Summary:
        //     Initializes a new instance of the Sana.Commerce.Web.Frontend.FrontendUrlHelper
        //     class.
        //
        // Parameters:
        //   requestContext:
        //     The request context.
        public FrontendUrlHelper(RequestContext requestContext);

        //
        // Summary:
        //     Gets the Sana URLs builder.
        public UrlsBuilder Sana { get; }

        //
        // Summary:
        //     Creates a new instance of the frontend URL helper initialized with the specified
        //     requestContext. It can be a standard helper class or a customized version if
        //     it is registered through the Sana.ObjectManager.
        //
        // Parameters:
        //   requestContext:
        //     The request context.
        //
        // Returns:
        //     Returns a new instance of the frontend URL helper.
        public static FrontendUrlHelper Create(RequestContext requestContext);
        //
        // Summary:
        //     Reads the selected facets from the query string of the current request.
        //
        // Parameters:
        //   facets:
        //     The collection of all available facets.
        public void ReadSelectedFacets(FieldFilterCollection facets);
        //
        // Summary:
        //     Sets the selected facets in the query string of the specified url.
        //
        // Parameters:
        //   url:
        //     The base URL.
        //
        //   facets:
        //     The facets.
        //
        // Returns:
        //     Returns the url with the selected facets in its query string.
        public virtual string SetSelectedFacets(string url, FieldFilterCollection facets);
        //
        // Summary:
        //     Reads the selected facets from the specified query string dictionary.
        //
        // Parameters:
        //   queryString:
        //     The query string.
        //
        //   facets:
        //     The facets.
        protected virtual void ReadSelectedFacets(NameValueCollection queryString, FieldFilterCollection facets);
        //
        // Summary:
        //     Sets the selected facets into the queryString.
        //
        // Parameters:
        //   queryString:
        //     The query string dictionary.
        //
        //   facets:
        //     The facets.
        protected virtual void SetSelectedFacets(NameValueCollection queryString, FieldFilterCollection facets);
    }

所以我的问题是:如何使用新方法扩展UrlsBuilder?

谢谢

另一种尝试是尝试这样:

public class V_ExtendendURlsBuilder : UrlsBuilder
    {
        public V_ExtendendURlsBuilder(RouteCollection routes) : base(routes)
        {
        }

        public virtual string BaliecodeVerkoper( string url)
        {

            return url;

        }



    }

但我仍然没有看到新的方法:

 public class ExtendFrontEndUrlHelper : FrontendUrlHelper
    {
        public UrlsBuilder builder;
        public ExtendFrontEndUrlHelper(RequestContext requestContext) : base(requestContext)
        {
        }

        public UrlsBuilder BalieCode()
        {

            return builder;

        }
    }

我只想用新方法Baliecode扩展UrlsBuilder类。这样我就可以在视图中使用该方法。但是UrlsBuilder是一个DLL。所以我必须使用新方法扩展该类。

我希望我现在很清楚。

查看我的图片。所以我仍然没有看到新方法Baliecode

谢谢

enter image description here

但是如何在视图中使用呢?

<a href="@Url." class="hyp">@Sana.SimpleText("ButtonText_baliecode")</a>

@Url ..然后??我没有看到扩展......

如果我去:

<a href="@Url.Sana" class="hyp">@Sana.SimpleText("ButtonText_baliecode")</a>

然后是@Url

上的f12

我看到这堂课:

public abstract class SanaWebViewPage : SanaWebViewPageBase
    {
        protected SanaWebViewPage();

        //
        // Summary:
        //     Gets the Sana view helper.
        public SanaViewHelper Sana { get; set; }
        //
        // Summary:
        //     Gets or sets the Sana.Commerce.Web.Frontend.FrontendUrlHelper object that is
        //     used to manage the URLs.
        //
        // Returns:
        //     The current Sana.Commerce.Web.Frontend.FrontendUrlHelper.
        public FrontendUrlHelper Url { get; set; }
        //
        // Summary:
        //     Gets the page info.
        protected virtual WebPageInfo PageInfo { get; }

        //
        // Summary:
        //     Initializes the helpers.
        public override void InitHelpers();
    }

哦,我现在拥有它,就像这样:

 public class extendedSanaWebviewPage : SanaWebViewPage
    {
        public ExtendFrontEndUrlHelper extendedFrontEndUrlHelper { get; set; }

        public override void Execute()
        {
            throw new NotImplementedException();
        }
    }

但是我没有在视图中看到:@extendedFrontEndUrlHelper

谢谢

1 个答案:

答案 0 :(得分:1)

假设您的public UrlsBuilder Sana { get; }实际上正在返回对V_ExtendendURlsBuilder实例的引用,您可以将其强制转换。 e.g。

var extendedUrlBuilder = (V_ExtendedURLsBuilder) Url.Sana;

extendedUrlBuilder.BaliecodeVerkoper("url");

然而,感觉可能总体而言不同的架构可能更好。 (对于架构重新评估,投射可以是线索,但不是强制性的)

编辑:在进一步澄清之后,或许你想要更像的东西:

public class ExtendFrontEndUrlHelper : FrontendUrlHelper
{
    V_ExtendedURLsBuilder builder;
    public ExtendFrontEndUrlHelper(RequestContext requestContext) : base(requestContext)
    {
        // presumably you assigned builder here somehow
    }

    public new V_ExtendedURLsBuilder Sana 
    { get { return builder; } }
}