VS2013 ASP.NET MVC单页应用程序模板自动重定向 [英] VS2013 ASP.NET MVC Single Page Application Template Auto Redirection

查看:483
本文介绍了VS2013 ASP.NET MVC单页应用程序模板自动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET默认模板SPA,在Visual Studio 2013为我们提供了一个默认的Web站点模板。然而,这种模板不允许看到/首页/索引页,并自动重定向到/帐号/登录。现在,我想利用这个不错的功能,但不能在我的主页。

我曾尝试以下内容:


  1. 注释掉授权属性

  2. 添加使用AllowAnonymous,OverrideAuthentication等一个接一个。

  // [授权]
[使用AllowAnonymous]
[OverrideAuthentication]
公共类HomeController的:控制器
{
    [OverrideAuthentication]
    [使用AllowAnonymous]
    公众的ActionResult指数()
    {
        返回查看();
    }
}

但它仍然重定向我


  

<一个href=\"http://example.com:49838/Account/Login?ReturnUrl=%2FAccount%2FAuthorize%3Fclient_id%3Dweb%26response_type%3Dtoken%26state%3D\" rel=\"nofollow\">http://example.com:49838/Account/Login?ReturnUrl=%2FAccount%2FAuthorize%3Fclient_id%3Dweb%26response_type%3Dtoken%26state%3D


每当我去


  

http://example.com:49838/Home/Index


我如何prevent呢?


解决方案

首先,删除从主控制器授权属性。

  // [授权]
公共类HomeController的:控制器

然后在/Views/Home/Index.cshtml删除SPA逻辑(我评论过它):

  @section SPAViews {
    @ Html.Partial(_家)
}
@section脚本{
    @ *
    @ Scripts.Render(〜/包/基因敲除)
    @ Scripts.Render(〜/包/应用程序)
    * @
}

基本上你正在做的是使主页一个普通的旧MVC页面,这是我猜你想做的事情。

然后就可以关闭链接到一个新的网页,其中将承载您的SPA应用程序(需要用户先登录)。

The default SPA template in ASP.NET, in Visual Studio 2013 provides us with a default web site template. However, this template does not allow to see /Home/Index page and automatically redirects to /Account/Login. Now, I do want to use this nice feature, but not on my home page.

I have tried the following:

  1. commenting out the Authorize attribute
  2. adding AllowAnonymous, OverrideAuthentication etc. one by one.

//[Authorize]
[AllowAnonymous]
[OverrideAuthentication]
public class HomeController : Controller
{
    [OverrideAuthentication]
    [AllowAnonymous]
    public ActionResult Index()
    {
        return View();
    }
}

But it still redirects me to

http://example.com:49838/Account/Login?ReturnUrl=%2FAccount%2FAuthorize%3Fclient_id%3Dweb%26response_type%3Dtoken%26state%3D

whenever I go to

http://example.com:49838/Home/Index

How can I prevent that?

解决方案

First, remove the Authorize attribute from the Home controller

//[Authorize]
public class HomeController : Controller

Then in /Views/Home/Index.cshtml remove the SPA logic (I've commented it out):

@section SPAViews {
    @Html.Partial("_Home")
}
@section Scripts{
    @*
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/app")
    *@
}

Basically what you are doing is making the Home page a regular old MVC page, which is what I'm guessing you want to do.

Then you can link off to a new page which will host your SPA application (and require the user to be signed in).

这篇关于VS2013 ASP.NET MVC单页应用程序模板自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆