I'm trying to create a contact form in Umbraco 9 CMS. I tried several solutions, wich all didn't work out. My last attempt was to make a view-file (Kontaktformular.cshtml), a controller (FormController.cs) and an integration in Startup.cs. When i send the form it sais: Could not find a Surface controller route in the RouteTable for controller name FormController. Now am at the point, where I don't know, what else I should do, to make that form work.
It would be great, if anybody could help me with that. You can see me code below.
Thank you!
Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Context;
using System;
using System.Configuration;
using System.Diagnostics;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Extensions;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Web;
using Umbraco.Cms.Web.Common.Controllers;
namespace Umbraco9.Web
{
public class Startup
{
private readonly IWebHostEnvironment _env;
private readonly IConfiguration _config;
/// <summary>
/// Initializes a new instance of the <see cref="Startup" /> class.
/// </summary>
/// <param name="webHostEnvironment">The web hosting environment.</param>
/// <param name="config">The configuration.</param>
/// <remarks>
/// Only a few services are possible to be injected here https://github.com/dotnet/aspnetcore/issues/9337
/// </remarks>
public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config)
{
_env = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
_config = config ?? throw new ArgumentNullException(nameof(config));
}
/// <summary>
/// Configures the services.
/// </summary>
/// <param name="services">The services.</param>
/// <remarks>
/// This method gets called by the runtime. Use this method to add services to the container.
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
/// </remarks>
public void ConfigureServices(IServiceCollection services)
{
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.Build();
services.AddRouting();
}
/// <summary>
/// Configures the application.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="env">The web hosting environment.</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStatusCodePages();
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseBackOffice();
u.UseWebsite();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "FormController",
pattern: "Form/{action}",
defaults: new { controller = "FormController", action = "Index" }
);
endpoints.MapControllers();
});
}
}
}
could it be this Html.BeginUmbracoForm("SubmitForm", "FormController", FormMethod.Post) normaly you would just refer to controllers by the name without the controller part, so Html.BeginUmbracoForm("SubmitForm", "Form", FormMethod.Post)
However this may cause a problem being refered to as Form, propably best to give it a different name, although it may be OK
Another think to try is change the definition of the SubmitForm action, I have just checked some of my code, and in places I had to do this
SubmitForm([FromForm] IFormCollection formModel)
To determine if that is the issue, you could change it to simple
`SubmitForm(Object formModel)`
if it then hits the controller it is the definition that is not correct for the object being passed. If it still doesn't hit the controller action then it is a routing issue. (you should not need to add a route to the startup.cs
Contact Form via Controller
Hello community
I'm trying to create a contact form in Umbraco 9 CMS. I tried several solutions, wich all didn't work out. My last attempt was to make a view-file (Kontaktformular.cshtml), a controller (FormController.cs) and an integration in Startup.cs. When i send the form it sais: Could not find a Surface controller route in the RouteTable for controller name FormController. Now am at the point, where I don't know, what else I should do, to make that form work.
It would be great, if anybody could help me with that. You can see me code below.
Thank you!
Startup.cs:
Kontaktformular.cshtml:
FormController.cs:
Hi,
could it be this
Html.BeginUmbracoForm("SubmitForm", "FormController", FormMethod.Post)
normaly you would just refer to controllers by the name without the controller part, soHtml.BeginUmbracoForm("SubmitForm", "Form", FormMethod.Post)
However this may cause a problem being refered to as Form, propably best to give it a different name, although it may be OK
Hi Huw
I also came up with that idea, but it didn't work out.
But thanks for your help!
try decalring your controller as a surface controller, does that make any difference?
Another think to try is change the definition of the SubmitForm action, I have just checked some of my code, and in places I had to do this
To determine if that is the issue, you could change it to simple
if it then hits the controller it is the definition that is not correct for the object being passed. If it still doesn't hit the controller action then it is a routing issue. (you should not need to add a route to the startup.cs
That also didn't work out. So it means, that there is a routing issue in my solution?
My firts attempt was to create a SurfaceController. But i had to much issues in dependencies and couldn't make it work...
It looks like it yes, one thing to check, when the form is rendered, what is it's action set to?
In the
That doesn't look right, it should be /Form/SubmitForm if that is your controller an action names.
I have had this issue before with forms in viewcomponents, try channging your form to the following
Hi Huw I tried again to edit my controller to a SurfaceController and now it seems to work! Thank you so much for your help!!!
is working on a reply...