In a brand new MVC5 project, I have a single html helper:
public static IHtmlString Localized(this HtmlHelper html, string url)
{
return /* code here */
}
In an empty page, I attempt to call this:
#{ ViewBag.Title = "Home Page"; }
#Html.Localized("~/content/images/mobile/hero.png");
And I get this error:
CS0121: The call is ambiguous between the following methods or properties: 'EUCA.HtmlHelpers.Localized(System.Web.Mvc.HtmlHelper, string)' and 'EUCA.HtmlHelpers.Localized(System.Web.Mvc.HtmlHelper, string)'
How is one method conflicting with itself?
This question has been answered here:
The call is ambiguous between the following methods or properties (bug??)
App_Code causes the code to be compiled twice, which doesn't seem to cause a problem in some basic cases. Extension methods an edge case that does not work with App_Code.
Related
I am trying to find the replacement for org.apache.struts.actions.DispatchAction.types in Struts2.
Below code snippet:
if(types != null)
{
if(types.length == forward.length)
{
select.add(type[0]);
}
}
The forward is a string type object and select is a list type array.
The documentation for DispatchAction.types:
types
protected java.lang.Class[] types
The set of argument type classes for the reflected method call. These are the same for all calls, so calculate them only once.
There's not the same thing in Struts2. You should refresh your mind to understand that struts2 works differently than struts-1.
For example you can map your actions directly to the method. You can use SMI for method call like in this answer
You should place the annotation directly on the method. Because if you put it on the class the default method execute() is used for the mapping.
#ParentPackage("basePackage")
#Namespace("/")
#AllowedMethods("test")
public class UserAction {
#Action(value = "userAction")
public String test() {
// your code
rerurn Action.SUCCESS;
}
}
or use wildcard mapping like this answer.
The action name is overridden if used in the same package. The action name maps to a specific method or execute.
You can use wildcard mapping to put a method name in the action.
<action name="*product" class="com.DispatchAction" method="{1}">
I'm using VB.NET MVC 5 with Identity 2.0.
I've been trying to configure my Startup.Auth to automatically use a single instance of ApplicationDbContext, CustomUserManager and CustomRoleManager per request as detailed in this tutorial.
My code is as follows: (minus garbage)
Public Sub ConfigureAuth(app As IAppBuilder)
app.CreatePerOwinContext(ApplicationDbContext.Create)
' Error 135 Overload resolution failed because no accessible 'CreatePerOwinContext' can be called with these arguments:
' Extension method 'Public Function CreatePerOwinContext(Of T)(createCallback As System.Func(Of Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions(Of T), Microsoft.Owin.IOwinContext, T)) As Owin.IAppBuilder' defined in 'Owin.AppBuilderExtensions': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
' Extension method 'Public Function CreatePerOwinContext(Of T)(createCallback As System.Func(Of T)) As Owin.IAppBuilder' defined in 'Owin.AppBuilderExtensions': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
app.CreatePerOwinContext(Of CustomUserManager)(CustomUserManager.Create)
' Error 136 Overload resolution failed because no accessible 'Create' accepts this number of arguments.
....
End Sub
But recieve these errors no matter what I do
Overload resolution failed because no accessible 'Create' accepts this number of arguments.
I think it's to do with me writing in VB and the example being in C#, though it infuriates me that this is a problem. My CustomUserManager is a Public Class, and the Create method is Public Shared.
Public Shared Function Create(options As IdentityFactoryOptions(Of CustomUserManager), context As IOwinContext) As CustomUserManager
Dim manager As CustomUserManager
manager = New CustomUserManager(New CustomUserStore(context.Get(Of ApplicationDbContext)()))
manager.UserValidator = New UserValidator(Of ApplicationUser, Integer)(manager)
manager.PasswordValidator = New PasswordValidator() With {
.RequiredLength = 6
}
manager.RegisterTwoFactorProvider("EmailCode",
New EmailTokenProvider(Of ApplicationUser, Integer)() With {
.Subject = "Security Code",
.BodyFormat = "Your security code is: {0}"
}
)
manager.EmailService = New EmailService()
manager.SmsService = New SmsService()
If Not options.DataProtectionProvider Is Nothing Then
manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser, Integer)(options.DataProtectionProvider.Create("ASP.NET Identity"))
End If
Return manager
End Function
Any ideas anyone? Any help is much appreciated, Cheers.
Try this
Public Sub ConfigureAuth(app As IAppBuilder)
app.CreatePerOwinContext(AddressOf ApplicationDbContext.Create)
End Sub
The AddressOf operator creates a function delegate that points to the function specified by procedurename
link1
link2
If you have already updated your visual studio 2013 with Update 3. This comes with all the identity stuff and custom user manager, role manager and many more.
Create new project using MVC template and Authentication selected as Individual User Accounts using Visual Studio 2013. Then you will get created all the code same as you have already implemented using the example. You can use that classes and implementation in order to fix your issues with your custom user manager.
Check the floder App_Start >> IdentityConfig.vb
Only thing I can see in your code, you have used integer as Id rather than string. That won't be a problem if you have correctly bind EF model biding stuff.
I am doing code coverage testing in Jmockit.When I was trying to cover an exception block in a class file with the code below
new NonStrictExpectations(){
{
someObject.executeProcedure("sometext",new ArrayList<SomeType>(),new SomeClass());
result=new ApplicationrunTimeException();
}
};
The exception block is not covered and the above code doesn't work.Whereas the code with slight modifications as shown below works well
new NonStrictExpectations(){
{
someObject.executeProcedure(anystring, (List<SomeType>)any,(SomeClass)any);
result=new ApplicationrunTimeException();
}
};
I dont know why is it so...
The actual code in the class file is
try
{
anotherObject=someObject.executeProcedure(SomeClass.STRING, someList,someClass);
}
catch(ApplicationrunTimeException exp)
{
}
in which SomeClass.STRING is a string,someList is of type List & someClass is a class variable..
I just want to know the usage of things like anyString,anyInt,any in Jmockit and in what way it differs from a valid string,integer and an object.
When your test runs SomeClass.STRING must not be equal to "sometext".
If you pass the string "sometext" into the method in the expectations block, then this is the value that must be provided during code runtime in order for the expectation to be invoked. Using anyString works because it will match any string.
I'm trying to write a test method for one of my controller methods.
Here is the controller method
public ActionResult LicenseDetails(Guid id)
{
var licenseDetails = _businessUnitRepository.GetLicenseDetails(id);
return View(licenseDetails);
}
and here is the test i've written to check if it calls the method in repository.
[TestMethod]
public void ModuleDetails_Action_Calls_GetLicenseDetails()
{
_mockBusinessUnitRepository.GetLicenseDetails(Arg<Guid>.Is.Anything);
_controller.LicenseDetails(Arg<Guid>.Is.Anything);
_mockBusinessUnitRepository.AssertWasCalled(x=>x.GetLicenseDetails(Arg<Guid>.Is.Anything));
}
I'm getting error right now that says:
Test method AdminPortal.Tests.Controller_Test.Customer.BusinessUnitControllerTests.ModuleDetails_Action_Calls_GetLicenseDetails threw exception:
System.InvalidOperationException: Use Arg ONLY within a mock method call while recording. 1 arguments expected, 3 have been defined.
Ps: I'm using Rhino mock and i'm new to this testing thing
I think it is complaining about this line:
_controller.LicenseDetails(Arg<Guid>.Is.Anything);
Instead of doing that, try passing in an actual value:
_controller.LicenseDetails(Guid.NewGuid());
i'm trying to inject stuff into a custom ViewPage (ModelViewPage, from MvcContrib)
public class ValidatedModelViewPage<T> : ModelViewPage<T> where T : class
{
public ValidatedModelViewPage(IEnumerable<IBehavior<IMemberElement>> elementBehaviors)
: base(elementBehaviors.ToArray()) { }
}
and my Autofac registrations look like this:
builder.RegisterCollection<IBehavior<IMemberElement>>().As<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<NotNullBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<StringLenghBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.RegisterGeneric(typeof(ValidatedModelViewPage<>));
but i get this error when i try to access a view:
Compiler Error Message: CS1729: 'UKFS.Web.Views.ValidatedModelViewPage<UKFS.Data.Entities.Skadeanmälan>' does not contain a constructor that takes '0' arguments
Source Error:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
Line 198: string[] dependencies;
Source File: c:\Users\Carl\AppData\Local\Temp\Temporary ASP.NET Files\root\be9ddc15\a84d5058\App_Web_edit.aspx.b2d4184a.thgwih90.0.cs Line: 196
i were clueless, but then i looked at App_Web_edit.aspx.b2d4184a.thgwih90.0.cs and found this:
Line 190: public class views_skadeanmälan_edit_aspx : UKFS.Web.Views.ValidatedModelViewPage<Skadeanmälan>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 191:
Line 192: private static bool #__initialized;
Line 193:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
of course, the generated class views_skadeanmälan_edit_aspx inheritates from my UKFS.Web.Views.ValidatedModelViewPage, and when it tries to instance it with the default construct.. so can you solve it?
You won't be able to constructor inject into ViewPages because the aspx compiler generates an empty ctor and as it derives from your base-class, your base-class must also have a empty ctor.
I'd look for property injection instead, otherwise try to accomplish the following:
Find the place where viewpages are instantiated, and get the Autofact there to instantiate the page
get the aspx compiler to not generate the empty ctor
I don't know how to do those things, so I'd aim for property injection instead