【问题标题】:React router consume my url request to back-end APIReact 路由器使用我的 url 请求到后端 API
【发布时间】:2019-04-19 11:35:03
【问题描述】:

我正在客户端构建一个 SPA react-redux 应用程序,asp.net 核心后端 API。 调试时,一切都与 IISExpress 完美运行。 但是,当我部署到 IIS 作为 Web 应用程序嵌套默认网站时,别名为“mysubdomain”。除了导出功能,一切都运行正常。

(第一种情况):打开浏览器,输入下载API链接:http://localhost/mysubdomain/api/v1/export?filterparam。保存对话框打开。这是我的期望。

(第二种情况:正常情况):打开我的网站(主页):http://localhost/mysubdomain然后点击导出,通过链接打开一个新窗口: http://localhost/mysubdomain/api/v1/export?filterparam。 我期待保存文件弹出窗口类似于(第一种情况)但NO。浏览返回我的 components/Layout 渲染。

我不知道反应路由器/路由发生了什么?还是我做错了什么?我猜 react-router 只是使用我的 URL 请求然后渲染我的组件,而不是调用我的后端 API。

我在 redux store 中的导出功能:

export: (filterParams) => async (dispatch, getState) => {
      const url = `api/v1/export?${filterParams}`;
      window.open(url,'_blank');
  }

后端 API:

[HttpGet]
[Route("download")]
public async Task<IActionResult> Download(DailyDischargeUrlQuery urlQuery)
{
  var stream = await _dailyDischargeRepository.ExportAsCsvStream(urlQuery.DischargeDate, urlQuery.MRN, urlQuery.GetCompanies(), urlQuery.GetOrders());

  return File(stream, "text/csv", "dailydischarge.csv");
}

index.js

const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const history = createBrowserHistory({ basename: baseUrl });
const initialState = window.initialReduxState;
const store = configureStore(history, initialState);    
const rootElement = document.getElementById('root');

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>,
  rootElement);

registerServiceWorker();

App.js

import 'core-js';
import React from 'react';
import {Route, Router} from 'react-router';
import Layout from './components/Layout';
import Home from './components/Home';

export default () => (
    <Layout>
        <Route exact path='/' component={Home}/>
    </Layout>
);

【问题讨论】:

    标签: reactjs rest api iis asp.net-core


    【解决方案1】:

    问题已解决! 那是因为 Service Worker 内置了 create-react-app。

    // In production, we register a service worker to serve assets from local cache.
    
    // This lets the app load faster on subsequent visits in production, and gives
    // it offline capabilities. However, it also means that developers (and users)
    // will only see deployed updates on the "N+1" visit to a page, since previously
    // cached resources are updated in the background.
    

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      相关资源
      最近更新 更多