Web Part Error -- The type is not registered as safe

I’ve been working on a SharePoint 2010 web part project and when I pressed F5 to debug, I ran across the error: “A Web Part or Web Form Control on this Page cannot be displayed or imported. The type is not registered as safe.”

webparterrorondebug

This can be caused by innocently renaming your assembly/namespaces/classes. The first thing to check is: SharePointProjectItem.spdata. Your web part should be properly referenced in the SafeControl section.


 
 
   
 

You can also get this error if you've added a web part to a page (e.g., debugging) and then the web part has been removed from the gallery.

If that doesn't resolve it, check your other files.

e.g. VisualWebPart1.webpart file:

 
VisualWebPart1.ascx.cs namespace name
namespace VisualWebPartProject2.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
   
VisualWebPart1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="VisualWebPartProject2.VisualWebPart1.VisualWebPart1UserControl" %>

VisualWebPart1.cs
namespace VisualWebPartProject2.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
   
Elements.xml

 
Assemblyinfo.cs
[assembly: AssemblyTitle("VisualWebPartProject2")]
...
[assembly: AssemblyProduct("VisualWebPartProject2")]