How to forbid View - asp.net-mvc-3

I have controller
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult _Nasljede()
{
return PartialView("Nasljede");
}
and Index action load _Nasljede action with javascript so when i put in browser address bar
..domain/controller#nasljede my page is working.(PartialView _Nasljede is loaded in Index View).
how can i forbid ..domain/controller?
is this even possible in mvc?
If you have any question please ask. i dont know if i explain my problem correctly.

I think this isn't possible with mvc. I solved my problem with jQuery.
if (!window.location.hash) {
window.location.replace("somewhere");
}

Related

static content pages in an MVC web application

I have just created my first MVC 3 project for a database search using EF db first, but the search is only a part of a big website most of the pages will just contain some text and images.
My question is basically about these pages which on the website would be .aspx, and the code behind would have nothing at all.
They use a master page and some user controls - my guess is that's the reason our front end person made them aspx not html.
I need to convert/include her pages into my project (I don't want to go back to stored procedures and listview after having used EF and Linq, plus I don't have time).
I know of one possible way: create a controller for each of the main menu items, then add ActionResult named for each of the submenu items returning View(), then create respective views.
public class LearnAboutStandardsController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ITSStandardsBackground()
{
return View();
}
public ActionResult ResearchInitiatives()
{
return View();
}
So my static content pages will become Views.
It's working, I just want to do it for the rest of the pages and modify the links in the text of these pages.
Is there any other way to handle these pages?
There is no logic behind these pages.
I know this was not a perfect project for the MVC pattern with so much static content, but I had my reasons for it.
I handle this with an "StaticContent" controller:
StaticContentController.cs
public class StaticContentController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Services()
{
return View();
}
public ActionResult Portfolio()
{
return View();
}
}
Add the code below your route config to handle the static routes:
routes.MapRoute(
"StaticContent",
"{action}",
new { controller = "StaticContent" },
new { action = "About|Services|Portfolio" } // Add more here
);
You're set.
If you need more pages just add the action in the StaticController and adjust your StaticContent MapRoute.
Personally, I would have controllers with simple actions that just render views. That way if you do add more features later you're already set up. And if you want to add security or caching it's a lot easier and more consistent.
You can still use WebForms (with the new Friendly URLs feature if you want "pretty" URLs) for the "static" pages. Or you can use Web Pages with Razor and create CSHTML files for the static content.

why TempData[] doesnt work with IE

İn my MVC3 project, there is plenty of TempData[] that I am using for passing datas between actions. And it works totaly perfect when I use Chrome. But in IE I can't get values of TempData[] items. if anyone knows whats the problem and how can I solve it?`
public class SomeController : Controller
{
public ActionResult SomeAction()
{
TempData["id"] = "someData";
return View();
}
}
public class AnotherController : Controller
{
public ActionResult AnotherAction()
{
string data = Convert.ToString(TempData["id"]);
return View();
}
}
`
You should never return a view from a controller action that stores something into TempData. You should immediately redirect to the controller action that is supposed to use it:
public class SomeController : Controller
{
public ActionResult SomeAction()
{
TempData["id"] = "someData";
return Redirect("AnotherAction", "Another");
}
}
public class AnotherController : Controller
{
public ActionResult AnotherAction()
{
string data = Convert.ToString(TempData["id"]);
return View();
}
}
The reason for this is that TempData survives only for a single additional request. So for example if inside the view you are sending an AJAX request to some controller action (no matter which) and then have a link in this view pointing to the target action, when the user is redirected to this target action TempData will no longer exist since it was lost during the AJAX request done previously.
If you need to store data for longer than a single redirect you could use Session.
If you need to store data for longer than a single redirect you should use Keep or Peek methods.
string data = TempData["id"].;
TempData.Keep("id");
or simply use,
string data = TempData.Peek("id").ToString();
Peek function helps to read as well as advice MVC to maintain “TempData” for the subsequent request.

cshtml file could not be found although it is under Shared folder

I have a cshtml under Shared folder. I am doing a RedirectToAction() to this page but its not looking for this file in Shared folder. Its only looking in the appropriate folder under views. It uses to look into Shared folder before and I have no idea what I could have changed that breaking lookup. Any ideas?
You cannot do a RedirectToAction to a view. You are doing (as it name suggests) a redirect to an action. It is this action that returns a view. By default it will look for views in the ~/Views/ControllerName and ~/Views/Shared. So let's suppose that you have the following action which performs a redirect:
public class HomeController: Controller
{
public ActionResult Index()
{
return RedirectToAction("Index", "Products");
}
}
which would redirect to the Index action on the Products controller:
public class ProductsController: Controller
{
public ActionResult Index()
{
return View();
}
}
Now the Index.cshtml view could be in ~/Views/Products/Index.cshtml or in ~/Views/Shared/Index.cshtml.

MVC3 Razor: Is it Possible to Render a Legacy ASCX?

With the Razor view engine in MVC3,
Is it possible to render a legacy ascx?
I was expecting to be able to do something like:
#Html.RenderPartial("Footer.ascx")
Yes. Try this instead:
#Html.Partial("Footer")
or
#{ Html.RenderPartial("Footer"); }
Just wanted to add that I haven't seen a lot of people posting this solution:
Html.RenderAction("Footer", "Home");
This is better practise if you are using MVC, because you can specify any data you need in the controller instead of trying to manage it in a free-floating partial view. Very beneficial if you use a BaseController class to initialize all your calls.
public class HomeController : Controller {
// ...
[ChildActionOnly]
public PartialViewResult Footer() {
// do work
return PartialView();
}
// ...
}

Weird MVC Issue

I have this code and I can't understand why it works this way
I have a model and view which is arbitrary and a very simple (but weird) controller
Here is my controller:
public partial class RouteController : Controller
{
[HttpGet]
public virtual ActionResult Create()
{
Create create = new Create();
return View("Create", create);
}
[HttpPost]
public virtual ActionResult Create(Create route)
{
return Create();
}
}
The first create method loads the view as normal. When the view posts back it runs the 2nd action which runs the first (as expected). The wierd part is the view is (re-)loaded with my previously entered data with errors (if any). I dont understand this because my model is empty. I was expecting it to post back with the same form as if it was loaded for the first time but with errors possibly.
Please explain.
That's the normal behavior of HTML helpers and it is by design. They first look at values contained in the ModelState and after that in the actual model. If you intend to modify some values on the model in a POST action you need to remove them from modelstate first:
For example:
[HttpPost]
public virtual ActionResult Create(Create route)
{
ModelState.Remove("SomeProperty");
route.SomeProperty = "some new value";
return View(route);
}
If you intend to completely modify everything as in your example you could clear the modelstate entirely:
[HttpPost]
public virtual ActionResult Create(Create route)
{
ModelState.Clear();
return Create();
}
Another possibility is to write your own TextBoxFor, HiddenFor, CheckBoxFor, ... helpers that will use the value in the model and not the one in the model state. Or yet another (non-recommended) possibility:
<input type="text" name="SomeProperty" value="#Model.SomeProperty" />
Of course in this case client validation among other things provided by the standard helpers won't work.

Resources