How to create own WebViewPage in mvc? - asp.net-mvc

I want to create own WebViewPage class so that i have created my class which is shown below
public abstract class iBlueWebViewPage<TModel>:WebViewPage
{
protected iBlueWebViewPage()
{
}
public HtmlHelper<TModel> iBlue { get; set; }
}
for example if we want text box in mvc we can code #html.TextBox("Name")
like that i need #iBlue.TextBox("Name") instead of #html
How can i achieve that ?
Thanks in advance..

You should change base type of pages, from web configuration (pages element)
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory,
System.Web.Mvc, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="MyNamespace.iBlueWebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
More detailed reading at Phil Haack's article (example is taken from there)

Related

Cannot implicitly convert type 'Kentico.Web.Mvc.IHtmlContentProxy' to 'System.Web.HtmlString'

Getting these error in my Razor viewpage:
HtmlString GetActionUrl()
{
return Url.Kentico().AuthenticateUrl(Url.Action("Upload", "ImageUploader", new
{
pageId = Context.Kentico().PageBuilder().PageIdentifier
}), false);
}
Added WebConfig:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing"/>
<add namespace="ResolutionDigital"/>
<add namespace="Kentico.Content.Web.Mvc"/>
<add namespace="Kentico.PageBuilder.Web.Mvc"/>
<add namespace="Kentico.Web.Mvc"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Am i missing some directives or references? Can anyone help? What am i missing?
Just adding my comment here that I solved the issue somehow.
I am using xperience 13 for the project. Probably i was having the version issues while adding the same code as kentico 12. But we can use this instead.
var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
string GetActionUrl()
{
return urlHelper.Kentico().AuthenticateUrlRaw(urlHelper.Action("Upload", "ImageUploader", new
{
pageId = Service.Resolve<IPageDataContextRetriever>().Retrieve<TreeNode>().Page.DocumentID
}), false);
}
I am sure you need to add some reference as well.
#using CMS.Core
#using CMS.DocumentEngine
Thankyou!

MVC 5 Change ViewEngine Path Error: (must derive from ViewPage) even with WebConfig in View Root

i try to move my Views default directory top a different path.
I have changed my Global.asax to add the directory in my view Engine like this
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
RegisterViewEngines(ViewEngines.Engines);
}
private static void RegisterViewEngines(ICollection<IViewEngine> engines)
{
engines.Add(new WebFormViewEngine
{
//MasterLocationFormats = new[] { "~/App/Views/Admin/{0}.master" },
PartialViewLocationFormats = new[] { "~/Mvc/Views/Shared/{1}/{0}.cshtml" },
ViewLocationFormats = new[] { "~/Mvc/Views/{1}/{0}.cshtml" }
});
}
This is the image of my new folder configuration
Every where i read i see that i need to add a web config to my Views root folder. So i take the one came in the template View and i paste it in my new folder configuration.
This is my /Mvc/Views/Web.Config
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Template" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>
No mater what i Always see this Error :
Error Stack Trace
The view at '~/Mvc/Views/Login/Index.cshtml' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.
Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code.
Détails de l'exception: System.InvalidOperationException: The view at '~/Mvc/Views/Login/Index.cshtml' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.
I have found my solution, the problem was in my RegisterViewEngine Function.
I use the Class WebFormViewEngine that is suppose to be used for file .ascx. for the Razor view you are suppose to use the Class RazorViewEngine
So i just have to change my function like this:
private static void RegisterViewEngines(ICollection<IViewEngine> engines)
{
engines.Add(new RazorViewEngine
{
//MasterLocationFormats = new[] { "~/App/Views/Admin/{0}.master" },
PartialViewLocationFormats = new[] { "~/Mvc/Views/Shared/{1}/{0}.cshtml" },
ViewLocationFormats = new[] { "~/Mvc/Views/{1}/{0}.cshtml" }
});
}
Now it work perfectly!

My namespace is not found in MVC 5 view

Im trying to create my own helpers but im totally stuck with this error. I have searched the web and read soooo many posts about this kind of issue.
When I try to add the namespace to the view I get the error "The type or namespace 'CustomHelpers' could not be found..."
So I created a class for my helper and added the namespace to the Views web.config.
My helper class
using System.Web.Mvc;
namespace CustomHelpers
{
public static class CustomHelpers
{
public static string Truncate(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{
return input;
}
else
{
return input.Substring(0, length) + "...";
}
}
}
}
My web.config
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Playground.Web" />
<add namespace="CustomHelpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
My View
#using CustomHelpers
#{
ViewBag.Title = "Home Page";
}
The problem is you should not name a class the same as its namespace.
Do not name a class the same as its namespace
For example,
namespace YourProjectName.Framework
{
public static class CustomHelpers
{
...
}
}

ASP.NET MVC: Referencing a model

I am setting up a new ASP.NET MVC 5 project in Visual Studio. My project is called MyProject. I have a model called MyModel, and a controller called MyController:
\\ MyModel.cs
namespace MyProject.Models
{
public class MyModel
{
public int Width { get; set; }
public int Height { get; set; }
}
}
\\ MyController.cs
using MyProject.Models;
namespace MyProject.Controllers
{
public class MyController : Controller
{
public ActionResult Index()
{
MyModel my_model;
return View(my_model);
}
}
}
However, in MyController.cs, I get the error message: The type or namespace 'MyModel' could not be found. Why is this? I have already included using MyProject.Models;, so shouldn't this be sufficient?
Thanks!
using MyProject.Models; should indeed be sufficient. Have you tried Rebuilding the solution (not build!) and restarting visual studio? My VS2013 needs a restart from time to time as well if I have been messing around with too many references.
Are you possibly receiving the error within your view? Try adding the namespace to the appropriate section within the web.config file found under your views folder.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="MyProject.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>

Register generic page base class

I create my own generic page base class:
public abstract class ViewPageBase<TModel> : WebViewPage<TModel>
{
public ParamBuilder<TModel> Param { get { return new ParamBuilder<TModel>(Model); } }
}
Web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="ViewPageBase">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
This entry causes the following errors:
INCORRECT_TYPE_PARAMETER_NUMBER
And
An expression tree may not contain a dynamic operation
How do I register a generic page base class with ASP.NET MVC4 without using the #inherits keyword in each Razor view?
This should work. The error you are getting is not related to the custom view page.

Resources