我需要在 MVC 中回发时返回两个 PDF 文件,但操作结果仅返回一个文件。有没有其他方法可以在一个动作结果中返回多个文件?
return File(stream, "application/pdf", Summary.pdf);
Run Code Online (Sandbox Code Playgroud)
像这样我需要返回多个文件。
我有一个使用 Owin Cookie 身份验证的 MVC 应用程序。我启用了 SlidingExpiration 并正常工作。但是,当用户的登录过期并将它们发送回 LoginPath 时,ReturnUrl 给我带来了一些问题:
我尝试创建自己的 AuthorizeAttribute(下面的代码)并将其应用于我的一个控制器中的某些方法,但似乎在会话过期时它永远不会被命中。
public class CheckLoginExpirationFilter : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
string returnUrl = null;
if (filterContext.HttpContext.Request.HttpMethod.Equals("GET", StringComparison.CurrentCultureIgnoreCase))
returnUrl = filterContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary()
{
{ "client", filterContext.RouteData.Values[ "client" ] },
{ "controller", "Security" },
{ "action", "Login" },
{ "ReturnUrl", returnUrl }
}); …Run Code Online (Sandbox Code Playgroud) 在我的程序中,控制器只是用一个函数挂钩按键。那么应该 ikeep 引用它吗?例如保持参考
Model model = new Model();
View view = new View(model);
Controller controller = new Controller(model,view);
Run Code Online (Sandbox Code Playgroud)
或没有
Model model = new Model();
View view = new View(model);
new Controller(model,view);
Run Code Online (Sandbox Code Playgroud)
内部控制器
public Controller(Model model, View view)
{
this.model = model;
this.view = view;
view.setOnKeyPressed(this::doSomething);
}
public void doSomething(KeyEvent event)
{
System.out.println("key pressed");
}
Run Code Online (Sandbox Code Playgroud)
也许我错误地实现了 Controller 类并且误解了 mvc 模式。但是到目前为止我写的内容没有理由让我保留对控制器对象的引用。
如果我将会话数据更改为数组(我是 CodeIgniter 中的新手),我不太确定为什么用户名没有出现在视图页面上。我有auth控制器来进行登录过程:
function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('Login');
# code...
}
else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$check = $this->M_login->check($username, $password);
// session data
if ($check->num_rows() == TRUE) {
foreach ($check->result() as $value) {
$sess_data['id'] = $value->id;
$sess_data['name'] = $value->name;
$sess_data['username'] = $value->username;
$sess_data['password'] = $value->password;
$sess_data['description'] = $value->description;
$this->session->set_userdata($sess_data);
}
redirect('Dashboard');
}
else {
$this->session->set_flashdata('result_login', '<br>Invalid username or password, try again.');
redirect('Login');
} …Run Code Online (Sandbox Code Playgroud) 我首先gradle bootRun使用以下控制器类成功构建了我的 Spring MVC 项目:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
public String hello() {
return "resultPage";
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将其更改为将数据传递给我的视图类:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
public String hello(Model model) {
model.addAttribute("message", "Hello from the controller");
return "resultPage";
}
}
Run Code Online (Sandbox Code Playgroud)
当我现在构建我的项目时,出现以下错误:
HelloController.java:13: error: cannot find symbol
public String hello(Model model) {
^
symbol: class Model
location: class HelloController
1 error
:compileJava FAILED
FAILURE: Build failed with …Run Code Online (Sandbox Code Playgroud) model-view-controller spring spring-mvc thymeleaf spring-boot
我将输入传递给 actionResult,它将使用它来创建一个新对象并使用 entityFramework 将其插入到数据库中,并且不想返回任何内容。
$(document).ready(function () {
$("#Add").click(function () {
var EmployeeeId = $("#EmployeeId").val();
var Phone =$("#Phone").val();
var Email = $("#Email").val();
var Address = $("#Address").val();
var UserModel =
{
"EmployeeId": EmployeeeId,
"Phone": Phone,
"Address": Address,
"Email": Email
}; $.ajax({url: '@Url.Action("AddNewContactList","Home")',
type: "POST",
dataType: "json",
data: JSON.stringify(UserModel),
contentType: 'application/json; charset=utf-8',
success: function (result) {
//Doesnt Matter
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Failed");
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器代码,控制器操作被调用
[HttpPost]
public ActionResult AddNewContactList(ContactList c)
{
DbContext m = new DbContext();
m.ContactList.Add(c);
m.SaveChanges();
return View(); …Run Code Online (Sandbox Code Playgroud) 我在 android 中使用 RecyclerView 时使用模型类、适配器类和活动。
我想知道这里的模型、视图和控制器是什么,为什么?
我正在使用模型视图控制器,并且在我的 URL 中,我需要在 URL 的末尾添加斜杠。如何检查它是否有斜线?
http://localhost/tabulation/event/event_name/<--I need to check this slash
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url. '/');
$url = explode('/', $url);
//if($url[2] has no backslashes then execute the condition
Run Code Online (Sandbox Code Playgroud) 我正在为我的 Java MVC 应用程序编写 REST 接口。这是一个函数:
@GetMapping(value = "/restaurant_representation", produces = MediaType.APPLICATION_JSON_VALUE)
public RestaurantRepresentation restaurantRepresent(
@RequestParam(value = "date", required = false) LocalDate date,
@RequestParam(value = "id") Integer id) {
return date == null ?
restaurantRepresentationCompiler.compileRestaurantRepresentation(id, LocalDate.now()) :
restaurantRepresentationCompiler.compileRestaurantRepresentation(id, date);
}
Run Code Online (Sandbox Code Playgroud)
现在我正在测试这个,并且
/rest/admin/restaurant_representation?id=1004
此请求提供了正确的结果,但是当我尝试添加日期参数时
/rest/admin/restaurant_representation?date=2015-05-05&id=1004
它显示了这一点:
客户端发送的请求在语法上不正确。
什么 LocalDate 格式是正确的?
我有提供全局错误处理的自定义中间件。如果捕获到异常,则应使用参考编号记录详细信息。然后我想将用户重定向到错误页面并只显示参考号。我的研究表明 TempData 应该是理想的,但它似乎只能从控制器上下文中访问。我尝试将参考编号添加到HttpContext.Items["ReferenceNumber"] = Guid.NewGuid();
但此值通过重定向丢失。
中间件如何通过重定向传递信息?我是否只需要将数字放在查询字符串中?
model-view-controller tempdata asp.net-core asp.net-core-middleware
asp.net-mvc ×2
c# ×2
java ×2
php ×2
ajax ×1
android ×1
asp.net-ajax ×1
asp.net-core ×1
codeigniter ×1
get ×1
javafx ×1
jquery ×1
localdate ×1
rest ×1
session ×1
spring ×1
spring-boot ×1
spring-mvc ×1
tempdata ×1
thymeleaf ×1