将参数发送到Web API时,参数值设置为0

时间:2018-07-24 11:08:35

标签: javascript c# asp.net-core asp.net-core-webapi

我有一个这样的函数来调用API

    log4j:ERROR setFile(null,true) call failed.
    java.io.FileNotFoundException: /Users/sgarretson/Desktop/mine/__a/source/ETClient/log.log (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:289)
    at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:167)
    at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:163)
    at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:256)
    at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:132)
    at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:96)
    at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:654)
    at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612)
    at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:509)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:415)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:441)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:470)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
    at org.apache.log4j.Logger.getLogger(Logger.java:117)
    at com.mchange.v2.log.log4j.Log4jMLog.getMLogger(Log4jMLog.java:51)
    at com.mchange.v2.log.MLog.getLogger(MLog.java:145)
    at com.mchange.v2.log.MLog.<clinit>(MLog.java:72)
    at com.mchange.v2.naming.JavaBeanReferenceMaker.<clinit>(JavaBeanReferenceMaker.java:39)
    at com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase.<clinit>(PoolBackedDataSourceBase.java:203)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:400)
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:275)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
    at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:103)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1475)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1443)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at com.ri.lambda.AbstractHandler.<init>(AbstractHandler.java:47)

并拥有API

async addOffDayReplacement(employeeId: string, days: number) {
    console.log(days);
    await Axios.post('/api/v1/leaveQuota/' + employeeId, {
        params: {
            additionalDays: days
        }
    });
}

但是即使我从服务发送的值不为0,我的API中的[HttpPost("{id}")] public async Task<ActionResult<bool>> Update(string id, int additionalDays) { await leaveService.AddReplacement(id, additionalDays); return true; } 始终为0

有人知道为什么吗?以及如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要指定绑定源,即您的函数如何访问它/如何传递参数(正文,查询等),因此请使用

[FromBody]int additionalDays

应该为您修复它。

string id起作用的原因是因为您在[HttpPost("{id}")]中指定了它。

我建议您查看https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1以获得更多详细信息和示例。

答案 1 :(得分:0)

使用一个对象作为包含AdditionalDays属性的第二个参数。用[FromBody]注释该参数。

[HttpPost("{id}")]
public async Task<ActionResult<bool>> Update(string id, [FromBody] Model model)
{
    await leaveService.AddReplacement(id, model.AdditionalDays);
    return true;
}

不要忘记使用AdditionalDays公共属性来定义Model类。

有关详细信息,请查看这篇文章:https://weblog.west-wind.com/posts/2012/Sep/11/Passing-multiple-simple-POST-Values-to-ASPNET-Web-API