asp.netreact返回index.html而不是json,如何解决?
创始人
2024-11-12 06:30:23
0

可能是应用程序未配置好或未正确设置路由。可以尝试以下措施:

  1. 在Startup.cs中,使用app.UseStaticFiles()方法将public文件夹添加为静态文件夹,并使用app.UseMvc()方法将路由设置为mvc:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
  1. 添加一个控制器和一个动作来处理请求,例如GetJson:
[Route("api/[controller]")]
[ApiController]
public class SampleController : ControllerBase
{
    [HttpGet]
    [Route("[action]")]
    public ActionResult GetJson()
    {
        var result = new { message = "Hello World" };
        return Json(result);
    }
}
  1. 在app.UseMvc()方法中,可以使用路由来指示应用程序如何响应请求。

例如,使用[HttpGet]和Route属性定义动作来指定路由参数:

[HttpGet]
[Route("[action]")]
public ActionResult GetJson()

这个动作将映射到/api/sample/getjson路由。

需要注意的是,这里的[ApiController]属性为了方便示例代码而添加的,实际应用中可以根据需要决定是否使用。

相关内容

热门资讯

透视数据!aapoker透视方... 透视数据!aapoker透视方法(透视)aapoker辅助器是真的吗,详细教程(有挂技巧)1、下载好...
透视真的!wepoker怎么挂... 透视真的!wepoker怎么挂飞机,wejoker辅助机器人(详细有人玩)1、完成wepoker怎么...
透视系统!aapoker真的假... 透视系统!aapoker真的假的(透视)aa poker透视软件,新版2025教程(有挂攻略);该软...
透视安装!wepoker轻量版... 透视安装!wepoker轻量版书签,wepoker私人局辅助器怎么用(详细靠谱);1、玩家可以在we...
辅助透视!aapoker怎么选... 辅助透视!aapoker怎么选牌(透视)aapoker怎么设置抽水,新2025版(有挂方法)1、任何...
辅助透视!wepokerplu... 辅助透视!wepokerplus透视脚本免费,淘宝买wepoker透视有用吗(详细科技辅助器)在进入...
透视游戏!aapoker怎么开... 透视游戏!aapoker怎么开辅助器(透视)aapoker俱乐部靠谱吗,wepoke教程(有挂详情)...
透视辅助!wepoker怎么提... 透视辅助!wepoker怎么提高运气,wejoker内置辅助(详细软件)透视辅助!wepoker怎么...
透视教学!aapoker免费透... 透视教学!aapoker免费透视脚本(透视)aapoker插件,德州论坛(有挂解密);透视教学!aa...
透视辅助!wepokerplu... 透视辅助!wepokerplus开挂,黑侠破解wepoker(详细作弊方法)暗藏猫腻,小编详细说明黑...