Replace nop 4.20 route (contact form) from plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Hi everyone,

Can anyone provide any insight on how to replace a nop route? I want to replace the ContactUs route with a custom page from within a plugin that I'm making. Or is replacing the route not even the way to go about this?

I have been looking through the forum for an answer, but I haven't found a definitive answer that mentions a working solution.

A concrete code example would be very helpful. Any help is much appreciated!
3 years ago
You can use
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
See https://www.nopcommerce.com/en/boards/topic/70457/how-to-override-view-on-nop-420
3 years ago
Thanks, problem solved!

Just as an easy reference for everyone, here is what I got (and modified) from the answer and link (specifically https://www.nopcommerce.com/en/boards/topic/70457/how-to-override-view-on-nop-420#242451).

---- Infrastructure/ContactFormViewEngine.cs ----

using System;
using Microsoft.AspNetCore.Mvc.Razor;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Plugin.Misc.MyContactForm.Infrasctructure
{
    public class ContactFormViewEngine : IViewLocationExpander
    {
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.AreaName == null && context.ViewName.Equals("ContactUs", StringComparison.InvariantCultureIgnoreCase))
            {
                viewLocations = new[] { "/Plugins/Misc.MyContactForm/Views/ContactForm.cshtml" }.Concat(viewLocations);
            }

            return viewLocations;
        }

        public void PopulateValues(ViewLocationExpanderContext context)
        {
        }
    }
}

---- Infrastructure/NopStartup.cs ----

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Infrastructure;
using Nop.Plugin.Misc.MyContactForm.Infrasctructure;

namespace Nop.Plugin.Misc.MyContactForm.Infrastructure
{
    public class NopStartup : INopStartup
    {
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ContactFormViewEngine());
            });
        }

        public void Configure(IApplicationBuilder application)
        {
        }

        public int Order => int.MaxValue;
    }
}
3 years ago
So, this allows me to replace the view with my own, but it is still using the base controller from Nop.Web. How would I be able to change the route so that it would use the controller of my plugin? Because I want to add new features to the form and some data to the model. Do I need a different interface for this?

The IRouteProvider won't let me do this, since it will complain about duplicate keys when trying to replace the ContactUs route.
3 years ago
Here are some bits and pieces that might help
Depending on the function - routes sometimes work or otherwise you need to override

Re duplicate key - you can make your own key

            routeBuilder.MapRoute("MyPlugin.CheckoutComplete", "checkout/completed/",
                new { controller = "MyCheckout", action = "Completed" });

Otherwise make an override of existing function - e.g. override of Checkout Completed

public partial class MyCheckoutController : CheckoutController
{
.......
    public override IActionResult Completed(int? orderId)
    {
......
     return View("~/Plugins/Payments.MyPlugin/Views/Completed.cshtml", model);
     }
}

Register it in DependencyRegistrar

    builder.RegisterType<MyCheckoutController>().As<CheckoutController>();
3 years ago
For the life of me, I don't know why I could not get the first proposal to work using the IRouteProvider, even after using a different key. It's working for me now so in any case thanks for providing both solutions.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.