C#测试WebApi项目调用DeepSeek(续)

  接着上一篇文章的WebApi项目,编写前端页面调用接口进行问答,使用jquery调用后台WebApi,页面内容使用DeepSeek生成后微调,提示词如下:

请使用html+css+JavaScript编写一个仿照微信聊天的页面,页面上方显示发送和收到的消息,发送的消息显示在右侧,收到的消息显示在左侧,页面下方是输入框和发送按钮。当消息一页显示不下时,支持滚动条显示

  前端页面调用WebApi时还得注意跨域的问题,需要在WebApi项目的program中填加如下代码:

...
...

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll",
        builder =>
        {
                    builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        });
});
...
...
app.UseCors("AllowAll");
...
...

  最后是页面运行截图及完整的前台页面代码:
在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微信聊天页面</title>
    <script src="..\jQuery\jquery-3.7.1.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .chat-container {
            width: 400px;
            height: 600px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        .chat-box {
            flex: 1;
            padding: 10px;
            overflow-y: auto;
            border-bottom: 1px solid #ddd;
        }

        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 5px;
            max-width: 70%;
            word-wrap: break-word;
        }

        .message.sent {
            background-color: #dcf8c6;
            margin-left: auto;
        }

        .message.received {
            background-color: #ececec;
            margin-right: auto;
        }

        .input-box {
            display: flex;
            padding: 10px;
            border-top: 1px solid #ddd;
        }

        #message-input {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
        }

        #send-btn {
            padding: 8px 16px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        #send-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-box" id="chat-box">
            <!-- 聊天记录将动态插入到这里 -->
        </div>
        <div class="input-box">
            <input type="text" id="message-input" placeholder="输入消息...">
            <button id="send-btn">发送</button>
        </div>
    </div>

    <script>
        document.getElementById('send-btn').addEventListener('click', sendMessage);
        document.getElementById('message-input').addEventListener('keypress', function (e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });

        function sendMessage() {
            const input = document.getElementById('message-input');
            const message = input.value.trim();
            if (message !== '') {
                addMessage(message, 'sent');
                input.value = '';                

                $.ajax({
                    type: 'GET',
                    url: "http://localhost:5052/Chat/Chat?question="+message,                                                         
                    success: function (result) {  
                        addMessage(result.msg, 'received'); 
                    },
                    error: function (result) {
                        addMessage("调用失败", 'received');         
                    }
                });
            }
        }

        function addMessage(text, type) {
            const chatBox = document.getElementById('chat-box');
            const messageElement = document.createElement('div');
            messageElement.classList.add('message', type);
            messageElement.textContent = text;
            chatBox.appendChild(messageElement);
            chatBox.scrollTop = chatBox.scrollHeight; // 滚动到底部
        }
    </script>
</body>
</html>

[1]微信公众号“技术老小子”,《C# 使用DeepSeek应用的完整指南》
[2]https://api-docs.deepseek.com/zh-cn/
[3]https://github.com/niltor/DeepSeekSDK-NET
[4]https://platform.deepseek.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值