Why does ASP.NET auto-generated .designer code hav

2019-02-14 06:53发布

问题:

I'm working on a hack to the DotNetNuke Events module. I've got the source set up and it built A-OK without modifications. However, when I change the EventMonth.ascx file, Visual Studio updates the .designer.vb file associated with it... and declares two objects with the wrong type! It looks like this:

Protected WithEvents EventIcons As Global.System.Web.UI.UserControl

And should look like this:

Protected WithEvents EventIcons As Global.DotNetNuke.Modules.Events.EventIcons

Obviously, this is not going to work... the compiler (rightly) throws an error where EventIcons is used in the code. What causes this? I could easily fix it manually, but then of course it will just break again later. I don't think it is the fact that the control's ID is the same as the class name, because the same thing happens with another instance of the same control, but with a different name.

Update: OK... I believe this is happening because the referenced user control cannot be found at design-time. But it obviously works at run-time:

<%@ Register TagPrefix="evt" TagName="Icons" Src="~/DesktopModules/Events/EventIcons.ascx" %>

This makes logical sense... but I guess the question then becomes "what does ~ resolve to at design-time?" I guess I can change this to simply "EventIcons.ascx" and it will generate code OK. But will it work at runtime? :|

回答1:

Because the project doesn't start at the application root, you have to tell the project where the real application root is before it will be able to resolve the ~ correctly. Go into the Web tab of the project's properties, and make sure that "Use Local IIS Web server" is selected under the "Servers" header. Set the Project URL to the URL to your specific project (i.e. http://localhost/DotNetNuke_2/DesktopModules/Events). Then, check the Override application root URL and set that to the application's root URL (i.e. http://localhost/DotNetNuke_2). This will allow the web application project to know how to find those controls.

The Src attribute is relative to the control, so it should work fine to just set it to "EventIcons.ascx" rather that specifying it from the root of the application.



回答2:

~ resolves to the root of the application folder.