Specify paths to actions in aspx pages MVC - asp.net-mvc

I'm working on ASP.NET MVC application. On my master page, I have a form that I need to submit to another action. Below is the relevant code:
<form id="form1" action="/Stock/PerformSearch" method="post" runat="server">
//some other stuff
</form>
As you can see, when I submit the form I want to go to the "PerformSearch" action in the "Stock" controller. This works fine on my local machine hosting. But when I try to publish the site on the server, MVC doesn't recognize this path anymore. My question is how should I specify the paths to actions in aspx pages such that it will work both locally and on live. I know that I can use the "using Html.BeginForm" and that will solve the problem, but I really don't want to use it for other reasons and also, I always need to specify paths to actions in many other tags other that form tag.
By the way, changing the path to "http://cse454db.cs.washington.edu/BestBet/Stock/PerformSearch"
will make it run on the server since the given path is the exact path for where the application will live (including the virtual directory) but I know this is not the perfect solution since I have to keep changing the path back and forth whenever running it locally or live. Also, I have the same problems in specifying the paths to actions in javascript files. I know there should be an easy solution for it, but I have been looking and couldn't find anything so any help will be greatly appreciated. thanks a lot,

I think you are confusing ASP.NET and ASP.NET MVC. You should use HTML helpers and never ever put a runat="server" tag in an ASP.NET MVC view. So your form could look like this:
<% using (Html.BeginForm("PerformSearch", "Stock")) { %>
//some other stuff
<% } %>
This will take care of generating the proper form tag.

Related

ReportViewer in MVC 4 partial

I am still unsure the best way to go about it.
I've read alsorts of resources, yet still no closer to a working solution.
I created a partial ASCX file. Added Report viewer to it, then rendered said partial in my CSHTML file. This was the closest I have come. In that I can see the report viewer, buttons etc. But for some reason the data never shows.
I have tried ASPX page inside an IFrame But this is not the way I want to go, about making this solution work.
Loading an ASPX page outright. But I lose my _Layout.cshtml main page style.
Some people suggest changing all sorts of things in config / Global.asax where as some say its not even necessary.
Ideally I want to have it as a partial in a page. But can deal with it being the only thing on the page, if I keep my main layout.
My experience with the older syntax / pages / non-MVC is limited to this project - trying to get this to work.
My data is linked through the components setup. The connection works fine in aspx page, as single page or iframe. But not in ascx.
The ReportView control requires ViewState to be enabled. In ASP.NET MVC such notion no longer exists. This means that you cannot use this control inside a native ASP.NET MVC view or partial. You can use it only in a classic WebForm (not in an ASP.NET MVC View) and the embed this WebForm inside an iframe within your current ASP.NET MVC view or partial. You have the possibility to render the report as an image for example and directly stream it to the response without using this control. Here's a blog post which illustrates the technique. So you could have a controller action which generates the report as a JPEG image and then link to this controller action from your view using the <img> tag. Unfortunately this makes only a static copy of the report and the user cannot interact with it.
You might also checkout this blog post which illustrates how you could run ASP.NET MVC and classic WebForms side by side.

How do I redirect requests for certain images in ASP.NET MVC 4?

I'm integrating a 3rd party jquery plugin into a page on my ASP.NET MVC website and I have found that it expects that the images are in an img folder relative to the page it is on. It generates img tags looking like this:
<img src="img/blah.jpg">
The trouble is, my page is at a URL like mysite.com/mycontroller/view/id and so there is no easy way of putting the img folder in the right place for it to pick up the images. I need the img tags to be like this:
<img src="/Content/img/blah.jpg">
Obviously I could edit the 3rd party javascript to output a different path every time it creates an img tag, but I am wondering whether there is a better way in ASP.NET MVC (perhaps with rerouting?).
(I am very new to ASP.NET MVC and web development, so please tell me if I'm going about this in completely the wrong way).
I would recommend avoiding trying to fix this via routing. It's going to be easier and most performant to edit the script to have the path be correct. Fixing it in the client script is likely as easy as a find and replace or just editing a single string object.

Jquery Templates with Razor how to use Razor within text/html scripts

Ok so this is a little random but..
Using MVC 3 (with Razor view engine) with Knockout.js which uses jQuery Templating i've come across a little problem i'm sure is possible to solve.
In order to use jQuery-Tmpl you need to supply a template in
<script type="text/html">
...template elements go here...
</script>
Now the problem is that the razor view engine doesn't seem to generate HTML inside of these specific script tags. It handles standard html, (script type="text/javascript") fine but appears to just not do anything with the aforementioned script tag.
Does anyone know how to get around this issue i.e. how to use MVC 3 Razor with jQuery-Tmpl?
There is a pretty good solution in this blog post: http://www.wiredprairie.us/blog/index.php/archives/1204
This creates a "template" helper that emits the script start/end tags.
Otherwise, I have some ideas for putting templates in external files, which would be another way to avoid this issue. It involves storing the templates in .html files and injecting them into the page into script tags. There are certainly many ways that this could be accomplished though on the client or server side, just a few ideas.
A more general approach if you want to keep things in the document is using #Html.Raw to output html without affecting the edit-time syntax state.
For example:
#Html.Raw("<script type='text/x-dot-template' id='awesome_template'>")
<!-- insert some awesomeness here -->
#Html.Raw("</script>")
I happen to like the helper method suggested above a little better, but it has not always been something I was able to implement, so this is an alternative with its own benefits (namely clarity over ease of use and terseness)

CSS include with MVC

So I know there are various ways of doing, however I'd like to know the "proper" way of including a specific CSS dynamically based on the page I am on. I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it, by either evaluating URL or passing a value through the controller for display flag, or just include it within the content page (outside of the head tags)... I'd like to keep it clean and link them all through my site master, but I'd like to be able to evaluate the page I'm on before I include that CSS..... thoughts??
No matter what its going to be something like this:
<% if( someCriteria ) { %>
<stylesheet type="text/css" href="mypath" />
<%} %>
You could wrap it in a helper or whatever but I don't think there can be a best practice or "cleaner" way of doing something this simple.
" I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it"
Another way to look at this is CSS files are cached by the browser so you may as well include it once and be done with. Your visitors may have a slightly longer initial load time but if you keep your CSS files small it will be barely noticeable. There is very little performance benefit by making it dynamic.
If you're using the Spark view engine, you can use the once attribute on your css include. I personally just put everything on the site master and let the browser handle caching.
http://sparkviewengine.com/documentation/expressions#Conditionalattributeonce
Good, bad, or indifferent, the thing I have been doing for years with master pages is include a ContentPlaceHolder in the <head> section of the master page. I can then inject page-specific CSS through that, instead of cluttering my master page with alot of processing logic. I am doing the same thing with my ASP.NET MVC solutions.
What I typically do is add a to the master in the to allow for pages to include things there. More often than not it is scripts rather than stylesheets, but it works for both.
I'd also vote for getting a Html helper in place to handle this so your developers don't need to care about exactly where the stylesheet is loaded from . . .

what is the best practices around links and url in asp.net-mvc

looking at different sites, i see conflicting conventions.
if you want to have links to images, other pages, js files, i have seen:
URL.Content("~/scripts/myscript.js");
<a href="/scripts/msscripts.js">
<img src="../../images/test.jpg" />
<img src="../images/test.jpg" />
<img src="/images/test.jpg" />
<img src="~/images/test.jpg" />
these all seem to work in asp.net mvc but it seems like there are all doing slightly different things.
I am moving to a new webserver where they are changing from IIS redirecting to isapi rewriting and i was told to make sure my links were done in a correct way or the site not work.
Can someone clarify what the "correct" way is ?
Use the first option. That way, you can relocate your application to any subdirectory or top level of your domain.
The second option is fine, if you know for sure that the application will NEVER run from a subdirectory (i.e. it will always be at the server's root).
The third and fourth are bad, because they refer to your URL structure, which is meaningless in a MVC environment: add a new bit to the path (one more argument for example) or refactor the paths, and nothing will work.
The best way is to use Url.Content, and make sure you have the ~ in it.
Url.Content will then replace the ~ by the correct path (the root of your website).
Only use relative paths in your css file.
If your css is located in
/css/style.css
and a background image in /images/background.png
then in your style.css use:
#divTest
{
background-image = url("../images/background.png")
}
Since your website gets moved around as a whole this will keep working.

Resources